# Combine lists from two lists of list into a single list of lists

def combine_in_place(first, second):
combined = []
maxLength = max(len(first), len(second))
for i in range(maxLength):
if len(first) < i+1:
combined.append(second[i])
elif len(second) < i+1:
combined.append(first[i])
else:
if not first[i]:
combined.append(second[i])
elif not second[i]:
combined.append(first[i])
else:
combined.append(first[i] + second[i])

return combined

print combine_in_place([['f'], ['j', 't'], [], ['s'], [], []], [['h', 'd'], [], [], []])
# prints: [['f', 'h', 'd'], ['d', 't'], [], ['s'], [], []]


So I know to program (been programming in Java for some time now). I am trying to learn Python but I am not sure what's the best way to do some stuff. My question is:
How to write the function in a more Pythonic way?

• The title should describe what the code does. Please edit it. Maybe you missed the placeholder on the title element: "State the task that your code accomplishes. Make your title distinctive.". Also from How to Ask: "State what your code does in your title, not your main concerns about it.". – Sᴀᴍ Onᴇᴌᴀ Jan 6 '20 at 22:41
• (Sorry for jumping in and coercing the title during suggested edit review.) – greybeard Jan 7 '20 at 7:35
• I know to program & combine_in_place([['f'], ['j', 't'], …], [['h', 'd'], …])& # prints: [['f', 'h', 'd'], ['d', 't'], …] hm, hm, hm, hm. – greybeard Jan 7 '20 at 7:37
• What does in_place mean in the name? Surely our definition differ as you are returning a new list. – 301_Moved_Permanently Jan 7 '20 at 12:53
• I'm fairly shocked that no-one's dropped the 1 line itertools way. It's just return map(chain.from_iterable, zip_longest(*args, fillvalue=())). – Peilonrayz Jan 7 '20 at 13:06

I would write something like that:

def combine(first, second):
ret_list = []
for i, j in zip(first, second):
if i is not None:
if j is not None:
return ret_list


I don't know if it is the most python-ish way but it is ok I think...

I mean you can also write something like this:

def combine(first, second):
return [(i.extend(j) if not (i is None or j is None) else i if j is None else j) for i, j in zip(first, second)]


But you cannot read and debug that. So I would strongly recommend against it.

Btw: Did not test the last one so I hope I did not forget a closing bracket ;)

Edit:

In order to keep all elements you have to add something to the end. The zip() function stops iterating after the shortest element reached its end. This is why the following method is needed instead of zip:

def combine(first, second):
ret_list = []
for i, j in itertools.zip_longest(first, second):
if i is not None:
if j is not None:
return ret_list


Note: Itertools simply must be imported first. (For Python2 it is itertools.izip_longest()...)

• This works but I want to keep empty lists at the end as well (which I did not make clear in my question). I edited my question to include that request. – Shehab Ellithy Jan 6 '20 at 22:05
• When I tested the combine function, it removed the empty ones at the end. – Shehab Ellithy Jan 6 '20 at 22:19
• @ShehabEllithy yeah forgot something. Wait a sec. – Cedric Jan 6 '20 at 22:20
• Perfect, that is what I needed. – Shehab Ellithy Jan 7 '20 at 6:43

Here's how I might do it:

from itertools import chain
from typing import List

def combine_in_place(*lists: List[List[str]]) -> List[List[str]]:
return [
list(chain(*[a[i] if i < len(a) else [] for a in lists]))
for i in range(max(len(a) for a in lists))
]


Some general principles I've attempted to follow:

1. When you're operating on multiple things, try to express them as a single iterable rather than individual named variables
2. If you can replace a bunch of individual if x < y with a single call to min() or max(), do that
3. It's generally preferable to build a list through comprehension rather than using extend/append.
• Since you reached for itertools, I’d use zip_longest with a fillvalue of [] or (). Then you can have the simpler [list(chain.from_iterable(elements)) for elements in zip_longest(*lists, fillvalue=[])] – 301_Moved_Permanently Jan 7 '20 at 13:01

The Pythonic way may include:
Note also how _in_place got omitted in Cedced_Bro's accepted answer.
No (previous) answer has suggested using/returning a generator or allowing an arbitrary number of lists, given the advantages, this, too, may be due to lacking documentation of combine_in_place().