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

Popular posts from this blog

How to check data type in C++? -

javascript - Is getTimezoneOffset() stable during daylight saving transition? -

sql server - SQL Query returns one result, does not return 0 or NULL result -