I've got something like:
list1 = [1, 3, None]
list2 = [2, 5, None]
And I'm looking to get something like:
merged_list = [12, 35, None]
P.S. Both lists will always have the same length and could have a None value as an element.
I've got something like:
list1 = [1, 3, None]
list2 = [2, 5, None]
And I'm looking to get something like:
merged_list = [12, 35, None]
P.S. Both lists will always have the same length and could have a None value as an element.
You can zip together the two lists and process each respective pair of elements.
[10 * a + b if a and b else None for a, b in zip(x, y)]
This list comprehension will combine a and b if neither are None. Otherwise, it will just put None.
Edit: If you just want to merge whatever values, do the same thing, but make sure you cast to a string so any value will work:
[str(a) + str(b) if a and b else None for a, b in zip(x, y)]
Note, it doesn't do the merge in place, but returns a new array with the combined values
You probably want to combine strings here. Something like
merged_list = []
for(i,j in zip(list1, list2):
if(element is not None):
merged_list.append(float(str(i) + str(j))
else:
merged_list.append(None)
Code not tested!