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,

name = [ "Venkat", "Nizar", "Akash", "Kishore" ]
roll_no = [ 4, 1, 3, 2 ]
 
# using zip() to map values
mapped = zip(name, roll_no)
 
print(set(mapped))

Output

For example, in zip enumerate,

names = ['Venkat', 'Akash', 'Kishore']
ages = [23, 35, 20]
 
for i, (name, age) in enumerate(zip(names, ages)):
    print(i, name, age)

Output

 For example, in zip Dictionary,

stocks = ['Wikitechy', 'Wipro', 'TCS']
prices = [2175, 1127, 2750]
 
new_dict = {stocks: prices for stocks,
            prices in zip(stocks, prices)}
print(new_dict)

Output

Leave a Reply

Your email address will not be published.

You May Also Like