1

I've been struggling with this problem for a couple of hours and I can't seem to reach a solution. So I have two lists of lists in python:

list1=[['= 0\n', '= 1\n', '= 2\n', '= 3\n', '= 4\n'],['= 0\n', '= 1\n', '= 2\n']]
list2=[['a', 'b', 'c', 'd', 'e'],['a', 'b', 'c']]

and I want to combine these two lists of lists into something like:

[['a=0\n', 'b=1\n', 'c=2\n', 'd=3\n', 'e=4\n'], ['a=0\n', 'b=1\n', 'c=2\n']]

Basically, I want to take each element from the first list in list1 and append the first element from the first list in list2 and so on and keep the list of list structure.

meow
  • 59
  • 10
  • 1
    I don't understand the output. Even ignoring the fact that you don't have quotes around your strings, why are `'a = 0\n'` and `'b=1\n'` inside a subsublist in the first sublist? How do you combine `' b '` with anything to get `'b=1\n'` without some kind of explicit stripping—and whatever the rule is, how does that same rule give you `'a = 0\n'` and `c= 2\n'`? – abarnert May 04 '18 at 17:30
  • Possible duplicate of [Sorting list based on values from another list?](https://stackoverflow.com/questions/6618515/sorting-list-based-on-values-from-another-list) – NaruS May 04 '18 at 17:30
  • 1
    Anyway, what I _think_ you're looking for is a nested zip. Do you know about [`zip`](https://docs.python.org/3/library/functions.html#zip)? If so, do you know how to write a `for` statement inside another `for` statement? (Or, if you prefer, a comprehension with two `for` clauses?) If so, what part are you stuck on? – abarnert May 04 '18 at 17:31
  • Why do some have spaces between them and some do not? – user3483203 May 04 '18 at 17:32
  • i fixed how the output should look. zip does not help me as it creates one list from the two, while I need a list of lists. – meow May 04 '18 at 17:33
  • @meow Your fix changes the list structure, but it still doesn't make any sense how the spacing is supposed to work. How can you get `'a = 0\n'` and `'b=1\n'` out of the same rule? – abarnert May 04 '18 at 17:35
  • @meow Meanwhile, did you miss the part about _nested_? If you want to process nested lists into nested lists, you need nested loops. – abarnert May 04 '18 at 17:36
  • I don't know how you still could see the spaces after I fixed the typos. I was writing this while on limited wifi, but I really needed an answer for this and wrote it as fast as I could and then cleaned it up. CoryKramer's and theausome's solutions were exactly what I needed. – meow May 04 '18 at 21:13

3 Answers3

2

Use a list comprehension with zip for pairing:

list1 = [['= 0\n', '= 1\n', '= 2\n', '= 3\n', '= 4\n'], ['= 0\n', '= 1\n', '= 2\n']]
list2 = [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c']]

print([[(x+y) for x, y in zip(list2[i], list1[i])] for i in range(len(list1))])
# [['a= 0\n', 'b= 1\n', 'c= 2\n', 'd= 3\n', 'e= 4\n'], ['a= 0\n', 'b= 1\n', 'c= 2\n']]                                  
Austin
  • 25,759
  • 4
  • 25
  • 48
1

You can first zip the outer lists together, then zip those elementwise and concatenate them into a single string.

>>> [[b+a for a,b in zip(i,j)] for i,j in zip(list1, list2)]
[['a= 0\n', 'b= 1\n', 'c= 2\n', 'd= 3\n', 'e= 4\n'], ['a= 0\n', 'b= 1\n', 'c= 2\n']]
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • The OP's desired output has the first two elements of the first sublist in another sublist; yours doesn't. Your space-handling rule only matches whatever the OP's desired rule is in one out of 8 examples. – abarnert May 04 '18 at 17:34
  • @abarnert They've updated their output, look again, that was a typo in their example – Cory Kramer May 04 '18 at 17:35
  • The spacing still doesn't match. The OP's output is probably _impossible_ to generate with any sensible code, but that just means the problem can't be answered, not that any answer is valid. – abarnert May 04 '18 at 17:37
  • Thank you very much for the help – meow May 04 '18 at 17:37
0

A variation of the previous answers using map:

list(map(lambda x: list(map(''.join, zip(*x))), zip(list2, list1)))
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45