What is zip () function in Python ?

  • In python zip () method takes container or iterable and returns a single iterator object, having mapped values from all the containers.
  • Zip () method is used tomap the similar index of multiple containers so that they can be used just using a single entity.
  • It returns a single iterator object, having mapped values from all the containers.

For example, in zip two lists,

[pastacode lang=”python” manual=”name%20%3D%20%5B%20%22Venkat%22%2C%20%22Nizar%22%2C%20%22Akash%22%2C%20%22Kishore%22%20%5D%0Aroll_no%20%3D%20%5B%204%2C%201%2C%203%2C%202%20%5D%0A%20%0A%23%20using%20zip()%20to%20map%20values%0Amapped%20%3D%20zip(name%2C%20roll_no)%0A%20%0Aprint(set(mapped))%0A” message=”” highlight=”” provider=”manual”/]

Output

For example, in zip enumerate,

[pastacode lang=”python” manual=”names%20%3D%20%5B’Venkat’%2C%20’Akash’%2C%20’Kishore’%5D%0Aages%20%3D%20%5B23%2C%2035%2C%2020%5D%0A%20%0Afor%20i%2C%20(name%2C%20age)%20in%20enumerate(zip(names%2C%20ages))%3A%0A%20%20%20%20print(i%2C%20name%2C%20age)%0A” message=”” highlight=”” provider=”manual”/]

Output

 For example, in zip Dictionary,

[pastacode lang=”python” manual=”stocks%20%3D%20%5B’Wikitechy’%2C%20’Wipro’%2C%20’TCS’%5D%0Aprices%20%3D%20%5B2175%2C%201127%2C%202750%5D%0A%20%0Anew_dict%20%3D%20%7Bstocks%3A%20prices%20for%20stocks%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20prices%20in%20zip(stocks%2C%20prices)%7D%0Aprint(new_dict)%0A” message=”” highlight=”” provider=”manual”/]

Output

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like