python - zip the values from a dictionary -
this question has answer here:
- zip 2 values dictionary in python 1 answer
i have dictionary in python 2.7 has following structure:
x = { '1': ['a', 'b', 'c'], '2': ['d', 'e', 'f'] } the length of value list same , zip value lists corresponding values. so, in case create 3 new lists as:
[['a', 'd'], ['b', 'e'], ['c', 'f']] i know can write awful looking loop wondering if there more pythonic way this. need preserve order.
you can following:
zip(*x.values()) explanation:
x.values()returns[['a', 'b', 'c'], ['d', 'e', 'f']](order may change might need sortxfirst.)zip([a, b], [c, d])returns[[a, c], [b, d]]- to expand
x.values()argumentszip, prepend*it.
Comments
Post a Comment