Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterate till the end of second array using python

I have done something like this:

d = [('e', 0), ('f', 1), ('e', 0), ('f', 1)]
e = ['a']
d = [(n,j) for n,(i,j) in zip(e,d)]
d
[('a',0)]

I was just tryig to replace the equivalent tuple value with the array value, without changing the associated numbers. But the list only goes till the len of array e and not d. What I want to get as output is something like this:

d
[('a', 0), ('f', 1), ('e', 0), ('f', 1)]
like image 805
Jaffer Wilson Avatar asked Jan 30 '23 21:01

Jaffer Wilson


2 Answers

Just add the unprocessed tail of d to the processed part:

[(n,j) for n,(i,j) in zip(e,d)] + d[len(e):]
#[('a', 0), ('f', 1), ('e', 0), ('f', 1)]
like image 115
DYZ Avatar answered Feb 02 '23 10:02

DYZ


You can use itertools.zip_longest:

[(n or i, j) for n,(i,j) in itertools.zip_longest(e, d)]

Check the doc

like image 38
Netwave Avatar answered Feb 02 '23 09:02

Netwave