If I have a list of 5 random integers, is there a simple way to iterate through the list and see how many of each number there are without using 25 if/elif statements. For example my list might contain [3, 7, 10, 10, 14]. How would I determine that there are two 10's and only one of everything else. I am still learning Python and don't know some of the shortcuts yet. Thanks.

1. Sort the list and test for this_number == next_number and add one to a counter if a duplicate.
2. Use a dictionary (if you are familiar with dictionaries) with the number as key pointing to a counter. Increment the counter every time that number is found.

Well this was fun, i made myself a little list comprehension to do this for me :)

#Your list is called f in my case 
>>> f = [150,2,16,5,5,1,3,2,1,3,6,6]
>>> print [str(g+1)+" : "+str(f.count(g+1)) for g in range(min(f)-1,max(f)) if f.count(g+1) > 0]
['1 : 2', '2 : 2', '3 : 2', '5 : 2', '6 : 2', '16 : 1', '150 : 1']

So you could appropriate that to do other things but at the moment it just outputs to the screen but im sure it could be used in other ways.

Oh and if there are huge differences between the lowest number and the highest number then maybe xrange would be more suitable. Also notice if there are no counts for the number then it is not added!

Wow, that was fun! :) Hope it helps someone

Comments
Thanks. i'll try that out

Thanks paulthom, that's awesome. Might take me a little while to wrap my brain around it, but I'm going to study it and see how it works and how I can implement it into my program. Thanks again!

This question has already been answered. Start a new discussion instead.