How to zip a Python Dictionary and List together?

PythonServer Side ProgrammingProgramming

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)]
raja
Published on 28-Feb-2018 12:04:53
Advertisements