Zip() in python by R4R Team

In Python 3, 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.
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!