Python zip () function add new sequence
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?
will say that 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 fine, however my data is not limited to data1 and data2. I can have up to 10 entries for each person. I tried to add datai`s to another data array [], however zip did not treat it as separate sequences.
data=[] data.append(data1) data.append(data2) zip(index,field,data)
gives no useful data as expected.
Appreciate any help for this.
+3
source to share
2 answers
Use *data
to unpack data:
In [1]: zip(index,field,data1,data2)
Out[1]:
[(0, 'name', 'john', 'david'),
(1, 'surname', 'nash', 'davidoff'),
(2, 'age', '88', '100'),
(3, 'gender', 'm', 'm'),
(4, 'location', 'konya', 'istanbul')]
In [2]: zip(index,field,*data)
Out[2]:
[(0, 'name', 'john', 'david'),
(1, 'surname', 'nash', 'davidoff'),
(2, 'age', '88', '100'),
(3, 'gender', 'm', 'm'),
(4, 'location', 'konya', 'istanbul')]
+3
source to share