tags:

views:

255

answers:

5

I'm new to Python and I have a simple question, say I have a list of items:

['apple','red','apple','red','red','pear']

Whats the simpliest way to add the list items to a dictionary and count how many times the item appears in the list.

So for the list above I would like the output to be:

{'apple': 2, 'red': 3, 'pear': 1}

+9  A: 
>>> L = ['apple','red','apple','red','red','pear']
>>> from collections import defaultdict
>>> d = defaultdict(int)
>>> for i in L:
...   d[i] += 1
>>> d
defaultdict(<type 'int'>, {'pear': 1, 'apple': 2, 'red': 3})
Adam Bernier
2 minutes, deserves a +1 :)
Justin Ardini
Probably the fastest and least-cluttered method.
Nick T
+1  A: 

One Liner:

l = ['apple','red','apple','red','red','pear']

print dict((item, l.count(item)) for item in set(l))

{'pear': 1, 'apple': 2, 'red': 3}
awesomo
I'm no Python expert, but wouldn't this be `O(N^2)` because of `count`?
Justin Ardini
He asked for simplest. This is about as simple as it gets.
awesomo
@Justin Ardini: I think so, O(n) for `count()` then O(n) for the list comprehension
Nick T
Technically it's O(nm), where m is the number of unique items in the list. Worst case m==n and it's O(n^2).
awesomo
Simplest and fewest number of lines aren't the same thing. The textbook defaultdict solution (Adam's) is simpler; it takes just a glance to understand.
Glenn Maynard
@Glenn Really worth a downvote? I wasn't equating 'number of lines' with 'simplest'. I think it's simple because anyone could read it and understand how it works. Given that the asker felt he needed help for an easy task such as this, I think simple is good. My two cents.
awesomo
+1  A: 
L = ['apple','red','apple','red','red','pear']
d = {}
[d.__setitem__(item,1+d.get(item,0)) for item in L]
print d 

Gives {'pear': 1, 'apple': 2, 'red': 3}

Nick T
+11  A: 

in 2.7 and 3.1 there is special Counter dict for this purpose.

>>> from collections import Counter
>>> Counter(['apple','red','apple','red','red','pear'])
Counter({'red': 3, 'apple': 2, 'pear': 1})
Odomontois
Yuck; enough narrow-purpose bloat in the Python library, already.
Glenn Maynard
Good community is implementing solution earlier than you are needing it.
Odomontois
The official line, or rather standing joke, is that Guido has a time machine ..
Muhammad Alkarouri
A: 

I always thought that for a task that trivial, I wouldn't want to import anything. But i may be wrong, depending on collections.Counter being faster or not.

items = "Whats the simpliest way to add the list items to a dictionary "

stats = {}
for i in items:
    if i in stats:
        stats[i] += 1
    else:
        stats[i] = 1

# bonus
for i in sorted(stats, key=stats.get):
    print("%d×'%s'" % (stats[i], i))

I think this may be preferable to using count(), because it will only go over the iterable once, whereas count may search the entire thing on every iteration. I used this method to parse many megabytes of statistical data and it always was reasonably fast.

stefano palazzo