Computer >> Computer tutorials >  >> Programming >> Python

How to zip a Python Dictionary and List together?


The zip() function can be used to zip one key-value pair from dictionary and correspoinding item in a list together

>>> dictionary = {'A':1, 'B':2, 'C':3}
>>> num_list = [1, 2, 3]
>>> zipped = zip(dictionary.items(), num_list)
>>> zipped
<zip object at 0x000000886641B9C8>

This zipped object when converted to list, shows following output

>>> list(zipped)
[(('A', 1), 1), (('B', 2), 2), (('C', 3), 3)]