You are here: Home / Topics / Zip() function in python

Zip() function in python

Filed under: Python on 2022-08-05 15:18:16

In Python, Zip() is an In-build function in which we pass two list as a argument and it will give or return the dictionary of python.
Basically it take two list and take items of first list as a key and items of second list as a corresponding values

Example - 

L1=[1,2,3]
L2=[4,,5,6]
ans= zip(L1,L2)
here ans is become a dictionary In python

Program -

l1=[1,2,3]
l2=[4,5,6]
ans=zip(l1,l2)
print(ans)
print(dict(ans))

l1=[1,2,3,4,5]
l2=[1,2]
ans=zip(l1,l2)
print(dict(ans))

output -

{1: 4, 2: 5, 3: 6}
{1: 1, 2: 2}

Note -
In first zip, the length of both the list is equal but in case of different length of both list, dictionary only contain the smaller length numbers item inside it.


About Author:
P
Pritvik     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.