programming-examples/python/Tuple/Python program to convert a list of tuples into a dictionary.py

6 lines
147 B
Python
Raw Normal View History

2019-11-15 12:59:38 +01:00
#create a list
l = [("x", 1), ("x", 2), ("x", 3), ("y", 1), ("y", 2), ("z", 1)]
d = {}
for a, b in l:
d.setdefault(a, []).append(b)
print (d)