Skip to content

Instantly share code, notes, and snippets.

@ebongzzang
Created October 4, 2018 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebongzzang/d5b1d5b858cdb867c37ad28d9dc5d18e to your computer and use it in GitHub Desktop.
Save ebongzzang/d5b1d5b858cdb867c37ad28d9dc5d18e to your computer and use it in GitHub Desktop.
nested list comprehesion, flatten and create dict from tuple list
subland_dict = {
'hi':['hi1','hi2','hi3'],
'hello': ['hello1', 'hello2', 'hello3']
}
res = [[(y,k) for y in v] for k, v in subland_dict.items()]
# [[('hi1', 'hi'), ('hi2', 'hi'), ('hi3', 'hi')], [('hello1', 'hello'), ('hello2', 'hello'), ('hello3', 'hello')]]
merged = dict(list(itertools.chain.from_iterable(res)))
# {'hi1': 'hi', 'hi2': 'hi', 'hi3': 'hi', 'hello1': 'hello', 'hello2': 'hello', 'hello3': 'hello'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment