Created
October 4, 2018 02:12
-
-
Save ebongzzang/d5b1d5b858cdb867c37ad28d9dc5d18e to your computer and use it in GitHub Desktop.
nested list comprehesion, flatten and create dict from tuple list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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