Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python zip() function adding a new sequence

Tags:

python

As you know zip() takes sequences as parameters and returns a list of tuples of elements mapped between those sequences. My question is : what if i have an undefined number of sequences?

lets say i have:

index=range(0,5)
field=['name','surname','age','gender','location']
data1=['john','nash','88','m','konya']
data2=['david','davidoff','100','m','istanbul']

if i use zip like below:

zip(index,field,data1,data2)

it works perfect, however my data is not limited to data1 and data2. I may have up to 10 records for each individual. I tried to append datai`s to another data[] array, however zip did not consider this as seperate sequences.

data=[]
data.append(data1)
data.append(data2)
zip(index,field,data)

gives no useful data as expected.

Appreciate any help for this.

like image 995
Krcn U Avatar asked Feb 09 '23 22:02

Krcn U


1 Answers

Argument list unpacking:

to_zip = [index, field, data1, data2]
zip(*to_zip)

Or:

to_zip = [data1, data2]
zip(index, field, *to_zip)

Or whatever combination you need.

like image 87
deceze Avatar answered Feb 15 '23 10:02

deceze