I looked at the following question. I wanted to do the same in Python.
List a=[ 1,2,3 none, none] list b=[4,5,3] Output=[1,4,2,5,3,3]
z = [ x for x in a if x != None ] + b
This does not work. I want the z to be [1,4,2,5,3,3]
I looked at the following question. I wanted to do the same in Python.
List a=[ 1,2,3 none, none] list b=[4,5,3] Output=[1,4,2,5,3,3]
z = [ x for x in a if x != None ] + b
This does not work. I want the z to be [1,4,2,5,3,3]
from itertools import chain
list(chain.from_iterable(zip([1, 2, 3, None, None], [4, 5, 6])))
zip(a, b)
as mentioned above will create a list of tuples, and chain.from_iterable
will flatten the list, discarding None
s
It looks like you want to chain
the lists after zip
ing them together and removing None
...
from itertools import chain, izip_longest
with_none = chain.from_iterable(izip_longest(a, b, fillvalue=None)]
without_none = [x for x in with_none if x is not None]
Use zip(a, b)
followed by flattening of list:
>>> [item for subtuple in zip(a, b) for item in subtuple]
[1, 4, 2, 5, 3, 3]