Advertisement
lzedl

Untitled

Mar 29th, 2019
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. something_dict = {
  2.     'a': [1, 2, 3],
  3.     'b': [4, 5, 6],
  4.     'c': [7, 8, 9],
  5. }
  6.  
  7. # 1. Initial
  8. something = sum([zip((k,) * len(v), v) for k, v in something_dict.iteritems()], [])
  9.  
  10. print something
  11.  
  12. # 2. Explicit variant
  13. def flatten_dict(d):
  14.     for k, values in d.iteritems():
  15.         for v in values:
  16.             yield k, v
  17.  
  18. print list(flatten_dict(something_dict))
  19.  
  20. # 3. Itertools variant
  21. from itertools import product, chain
  22.  
  23. print list(chain.from_iterable(product(k, v) for k, v in something_dict.iteritems()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement