I have a dictionairy
d = {0: ['LON', 'SFO', 'LIS'], 1: ['MAD', 'LAX', 'ROM'], 2: ['BOS', 'SAO', 'SLC']}
That I want to subset for all the keys that are 0 or 1.
I tried:
[v for k, v in d.items() if k in [0, 1]]
But this returns
[['LON', 'SFO', 'LIS'], ['MAD', 'LAX', 'ROM']]
Is there anything I could add to the subset line that would return a flat list?
CodePudding user response:
You could change your list comprehension to something like:
[item for k,v in d.items() if k in [0,1] for item in v]
CodePudding user response:
Is there anything I could add to the subset line that would return a flat list?
So you just want to flatten your list?
flatten_list = sum([element for k, v in d.items() if k in [0, 1] for element in v])