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.
txwooley
0
Junior Poster in Training
Recommended Answers
Jump to Post1. 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.
All 4 Replies
woooee
814
Nearly a Posting Maven
txwooley
0
Junior Poster in Training
lllllIllIlllI
178
Veteran Poster
txwooley
commented:
Thanks. i'll try that out
+1
txwooley
0
Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.