dictionary = {1: ['a', 'b'], 2: ['a'], 3: ['b', 'c']}
I want this dictionary to be in list of tuple like this: output:
[(1, 'a'),(1, 'b'),(2, 'a'),(3, 'b'),(3, 'c')]
Please help me out in this!!!
CodePudding user response:
You can do this with a comprehension:
[(x, z) for x, y in dictionary.items() for z in y]
Or expanded out:
out = []
for x, y in dictionary.items():
for z in y:
out.append((x, z))
CodePudding user response:
dictionary = {1: ['a', 'b'], 2: ['a'], 3: ['b', 'c']}
list_of_tuples = []
for k,v_list in dictionary.items():
for v in v_list:
list_of_tuples.append((k,v))
list_of_tuples