Concatenating dict values, which are lists
You nearly gave the answer in the question:
sum(test.values())
only fails because it assumes by default that you want to add the items to a start value of 0
—and of course you can't add a list
to an int
. However, if you're explicit about the start value, it will work:
sum(test.values(), [])
Use chain
from itertools
:
>>> from itertools import chain
>>> list(chain.from_iterable(test.values()))
# ['sunflower', 'maple', 'evergreen', 'dog', 'cat']
One liner (assumes no specific ordering is required):
>>> [value for values in test.values() for value in values]
['sunflower', 'maple', 'evergreen', 'dog', 'cat']