1

I have a dictionary with the following format

{'bye': ['yellow', 'green', 'orange', 'purple'],
 'hello': ['red'],
 'hi': ['red', 'blue']}

What I'm trying to do is count all the values in this dictionary, no matter what the key is, no matter if the values have the same name, if its there count it.

So far i have

mynumber = 0
for key, value in mydict.iteritems():
  mynumber +=1
return mynumber

This output only give me the total number of keys, what i want is the sum of all values, repeated or not. So in this instance mynumber should be 7

1 Answer 1

3

You can pass a generator to sum():

total = sum(len(item) for item in mydict.itervalues())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.