Find Unique Values in List (Python)

I Googled this problem today and found a lot of people creating long-ish functions to find unique values. There is a much simpler solution: turn your list into a set (which removes duplicates) and then back into a list.

newList = list(set(oldList))

Hope this is of help to someone. Apologies if this is already a well-known solution. Perhaps “set” was unavailable in earlier (pre-2.7) versions of Python.

Update: This post generates a fair amount of traffic. Peter Bengtsson has some other options if you care more about speed/ordering/etc rather than simplicity, which was the goal here.

New Year’s Resolution: Get Addicted

While many are trying to kick old habits or shed holiday pounds, I offer you a different challenge: pick up a habit. While I wouldn’t encourage smoking or competitive eating, there are a lot of positive addictions out there. As Paul Graham points out, addiction can serve as another word for something we like too much.

You can always make time for the things you truly love. The things you spend your time on reveal a lot about what you prefer. Probably more so than the things you say, “I wish had time for….” So do it. Think of something you’ve always wanted to do. Sit down and figure out how an hour or two per day for a month can get you closer to that goal.

For further motivation, here is Google’s Matt Cuts encouraging you to expand your horizons for thirty days:

Micro-Institutions Everywhere: Pedestrians

From The Economist, two researchers trying to understand how crowds behave:

Imagine that you are French. You are walking along a busy pavement in Paris and another pedestrian is approaching from the opposite direction. A collision will occur unless you each move out of the other’s way. Which way do you step?

The answer is almost certainly to the right. Replay the same scene in many parts of Asia, however, and you would probably move to the left. It is not obvious why. There is no instruction to head in a specific direction (South Korea, where there is a campaign to get people to walk on the right, is an exception). There is no simple correlation with the side of the road on which people drive: Londoners funnel to the right on pavements, for example.

This reminds me of a story that my friend Chris likes to tell (apologies if I mess up any details). One day when his sister was in college, she was riding her bike down the sidewalk and had to turn a corner. There was another biker coming the opposite way. They both swerved. She “naturally” went to the right and he went to the left–but it was his left, her right. Crash. The other biker was from South Africa, where they drive on the left.

Until I read the above, I had assumed that people walked the way that they drive, but it appears that I was wrong. It will be interesting to see how South Korea’s efforts to overturn the emergent order work out. At the moment I can’t actually recall the dominant walking pattern in Jamaica, where they drive on the left, but in any case it is probably confounded by the high numbers of American tourists. Personally I just wish people would be a little more careful taking sharp lefts around corners.

Wednesday Nerd Fun: The Game of 99

This week’s entertainment is an original creation–my first computer game. The game is based on the board game “99″ (French: “Le Jeu de 99″) that my girlfriend’s family introduced me to a couple of summers ago.

I made the game for several reasons. The first was to improve my very modest Python programming skills–this was my first program to include a GUI. An earlier version was text based, and it was very ugly–email me for the source code if you want to see just how ugly. The second reason was the irony of turning an old-fashioned Mennonite game into a computer game. Perhaps I will try to preserve other antique games this way in the future. My third motivation was to create a multi-player online version, but that will have to wait.

Game play proceeds as follows. On each turn, the player (total number: two or three) can draw a card, discard, skip their turn, or play. The cards are numbered with a range: the largest is 0 to 99, the smallest is 99 to 99. This range specifies the number spots on the board onto which the player can place their marker. You may have up to five cards in your hand at a time. The game allows players to hide or show their hands so that their competitor may not see them. When you choose to play, you may place your marker on any open spot within the range specified by the card that you play. The game is won when a player places five of their markers in a row horizontally, vertically, or diagonally. (Full instructions.)

Opening screenshot

The game does not currently have a function to tell when a player is won, so you’ll have to do that visually for now. This is the first update that I hope to add, along with a “replay” button. As I said above, I’d also like to make the game playable online, which will probably mean learning Django.

Thanks to Jennifer for her help as a beta tester. You can download the source code for the game here or the Mac OS X app version here. Please notify me of any problems with the game–developers are welcome to modify the source themselves under a Creative Commons license.

Update on SOPA

This post is a follow-up on an earlier one, which can be found here.

Yesterday, Go Daddy, the world’s largest registrar of internet domain names, released a statement in support of a revised version of SOPA. I immediately started seeing tweets of bloggers and entrepreneurs looking to move away from Go Daddy. This follows  at least a week of other major websites showing their opposition to SOPA. 

An informal poll initiated yesterday asked, “Did you transfer your domains from GoDaddy today?” Although this poll fares very poorly by the standards of public opinion research, the results are somewhat interesting. As of noon CST today, the poll had 951 respondents. 39% of respondents said “yes,” and another 48% responded “not yet, but will transfer soon.” Only 13% of those who took the poll–who were presumably aware of “recent Go Daddy and SOPA news”–indicated no plans to transfer away from Go Daddy.

It will be interesting to see whether people actually march with their (virtual) feet and (real) dollars in support of this policy position.

Further reading: 

Dear Internet: It’s No Longer OK to Not Know How Congress Works

Dear Congress, It’s No Longer OK to Not Know How the Internet Works (original page appears to be down, maybe because of high traffic; see quotes here and here)

Dead Congress: It’s Not OK Not to Know How Search Engines Work Either

Oh, and in case you want to express your opinion to your congressman on this issue, I hear that they still accept telegrams.

Update to the update: (1:34pm CST)

Go Daddy no longer supports SOPA.

Go Daddy will support it when and if the Internet community supports it…. In an effort to eliminate any confusion about its reversal on SOPA though, Jones has removed blog postings that had outlined areas of the bill Go Daddy did support.

Way to go, Internet. (via @newsyc20)

Are Assassins Crazy?

The answer to this probaby depends on the image that pops into your head when you hear the word “assassin,” which in turn probably depends (for American readers) on whether you’re old enough to remember the JFK assassination or the attempt on Ronald Reagan’s life by John Hinckley. It turns out that this question has been asked for over 150 years. One of the earliest answers was called the M’Naghten rule (pronounced–and sometimes spelled–”McNaughton”).

The M’Naghten rule resulted from the attempted assassination of Robert Peel by Daniel M’Naghten. His case was decided by a specific series of criteria, the most important of which was whether or not the individual knew that what he was doing was wrong. This rule was the primary legal doctrine for the insanity defense in the Anglophone world until at least 1953, when it was judged to be obsolete.

I became interested in this history when reading Destiny of the Republic, about the assassination of James Garfield. I won’t go into details about his case, but they are interesting in their own right and the book is highly recommended. Allow me to share the author’s description of the rule’s impact, which reminded contemporaries of an earlier attempt on Queen Victoria’s life by Edward Oxford:

“We have seen the trials of Oxford and MacNaughtan conducted by the ablest lawyers of the day,” Queen Victoria had written in disgust to Peel after the M’Naghten ruling, “and they they allow and advise the Jury to pronounce the verdict of Not Guilty on account of Insanity,–whilst everybody is morally convinced that both malefactors were perfectly conscious and aware of what they did!” Before her eventual death in 1901, at the age of eighty-one, Queen Victoria would survive several more assassination attempts. Her husband, who had lived to witness four of them, was convinced that the would-be assassins had been encouraged by Oxford’s acquittal.

This last part strikes me as interesting because of its irony: supposedly insane individuals were thought to be more likely to attempt assassinations when the likelihood of punishment was reduced.*

What does this mean for contemporary political science? Over the past 35 years, and particularly in the last decade and a half, the question of whether assassins, terrorists, insurgents and the like are rational has been a critical but mostly unproven assumption in empirical research on political violence. If it turns out that assassins are more likely to strike in countries where the M’Naugton rule was in effect, this would suggest (but obviously not prove) that assassins respond to changing costs and benefits of their actions.

Further reading: Experts Disagree on Psychological State of Norwegian Killer (via @intelwire)

__________________________________

* Note: I am not in any way advocating for a lack of legal sympathy for mental conditions. This post is meant to demonstrate the possible rationality of a supposedly irrational act.

Wednesday Nerd Fun

This should be enjoyable for game theorists, programmers of basic games, and those who love translations of obscure French novels.

The Art of Asking Your Boss for a Raise Interactive Flowchart

If you fall into none of the categories above, your flowchart has probably already sent you away from this page. More fun to come as Christmas break is officially on.

US Drug Prices, 2004-2008

Apologies for the dearth of posts lately. Its finals time and I’m trying to get a paper or two out the door. I yearn for the days when journals will print in color, or better yet be all-online so it won’t matter and we could even include animations. For now, here are a couple boring charts of wholesale and retail drug prices in the US. Does it appear that the efforts against Mexican DTO’s have really hurt the market?

Source is the UN Office on Drug Control. Dashed lines are wholesale prices, solid lines are retail prices (both in per-gram units). More to come over the break.