I don't understand why this list comprehension isn't correct. If I understand list comprehensions correctly, it should first iterate over the dictionaries in the list [dict1, dict2] and then over key-value pairs in each dictionary and return the values. Were am I wrong?
dict1 = {'a' : 1, 'b' : 2, 'c' : 3}
dict2 = {'c' : 2, 'd' : 3, 'e' : 4}
[value for key, value in d.items()
for d in [dict1, dict2]]
It gives the following error:
NameError Traceback (most recent call last)
<ipython-input-78-e8d31c1fb24a> in <module>()
1 dict1 = {'a' : 1, 'b' : 2, 'c' : 3}
2 dict2 = {'c' : 2, 'd' : 3, 'e' : 4}
----> 3 [value for (key, value) in d.items()
4 for d in [dict1, dict2]]
NameError: name 'd' is not defined
I would expect:
[1,2,3,2,3,4]
d.values()
instead ofd.items()
which just clutters your code, if you are only interested in values... – juanpa.arrivillaga[val for d in (dict1,dict2) for val in d.values()]
– juanpa.arrivillaga