+ 3

What is zip() used for in tuple concept

Zip()

25th Jun 2020, 4:01 AM
kireeti
kireeti - avatar
2 odpowiedzi
+ 3
Zip function takes two arguments and return list of tuples lis2 = [x for x in zip([2,3,4],[1,2,3])] print(lis2)
25th Jun 2020, 4:57 AM
uday kiran
uday kiran - avatar
+ 1
''' zip function transposes the lists in the arguments *it can take any number of arguments *it returns tuplea ''' ''' suppose if u want sum of first element lof following four list u can transpose and find it easily using zip ''' list1=[1,2,3] list2=[5,6,7] list3=[8,9,9] list4=[4,3,7] list5=list(zip(list1,list2,list3,list4)) print(list5) print("sum of first element of list 1 to 4 is equal to",sum(list5[0])) ''' if you have list of list and want to return a transpose of it you can make use of star operator to unpack list and ise map funtion to convert tuple to list ''' lol=[[1,2,3],[5,6,7],[8,9,9],[4,3,7]] transloltuple=list(zip(*lol)) translollist=list(map(list,transloltuple)) print(translollist)
25th Jun 2020, 9:32 PM
Suhail KM