python - zip the values from a dictionary -


this question has answer here:

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 sort x first.)
  • zip([a, b], [c, d]) returns [[a, c], [b, d]]
  • to expand x.values() arguments zip, prepend * it.

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -