So my aim is to go from:
JavaScript
x
2
1
fruitColourMapping = [{'apple': 'red'}, {'banana': 'yellow'}]
2
to
JavaScript
1
2
1
finalMap = {'apple': 'red', 'banana': 'yellow'}
2
A way I got is:
JavaScript
1
5
1
from itertools import chain
2
fruits = list(chain.from_iterable([d.keys() for d in fruitColourMapping]))
3
colour = list(chain.from_iterable([d.values() for d in fruitColourMapping]))
4
return dict(zip(fruits, colour))
5
Is there any better more pythonic way?
Advertisement
Answer
JavaScript
1
4
1
finalMap = {}
2
for d in fruitColourMapping:
3
finalMap.update(d)
4