tags:

views:

714

answers:

2

I have this bit of code:

    visits = defaultdict(int)
    for t in tweetsSQL:
         visits[t.user.from_user] += 1

I looked at some examples online that used the sorted method like so:

sorted(visits.iteritems, key=operator.itemgetter(1), reverse=True)

but it's giving me:

"TypeError: 'builtin_function_or_method' object is not iterable"

I am not sure why

+5  A: 

iteritems is a method. You need parenthesis to call it: visits.iteritems().

As it stands now, you are passing the iteritems method itself to sorted which is why it is complaining that it can't iterate over a function or method.

A: 

Personally I think one of these forms is a little more succinct as the first argument only needs to be an iterable not an iterator.

sorted_keys = sorted(visits.keys(), reverse=True)
sorted_keys = visits.keys().sort(reverse=True)
mattkemp
He wants to sort on value, not on key
miles82
You're correct, I misread the question.
mattkemp