python - Creating tuple pairs from a dictionary that has a list as value -
i trying list of tuple pairs dictionary has list value.
d={ 'a': [1,2], 'b': [4,5], 'c': [7,8] } print(d.items()) >>[('a', [1, 2]), ('b', [4, 5]),('c', [7, 8]) ] how list in form
[('a', 1),('a', 2), ('b',4),('b',5),('c',7),('c',8)]
using simple list comprehension:
d = {'a': [1,2,3], 'b': [4,5,6]} l = [(k, v) k in d v in d[k]] print(l) # => [('a', 1), ('a', 2), ('a', 3), ('b', 4), ('b', 5), ('b', 6)] there's other ways it, simplistic , doesn't require other libraries.
Comments
Post a Comment