0

Suppose I have the following lists:

list_1 = [[1,2],[3,4],[5,6],[7,8]]
list_2 = [11,12,13,14]

How can I add items from the second list to each of the items from the first one?

Stated clearly, this is the result I'm looking for:

desired_output = [[1, 2, 11], [3, 4, 12], [5, 6, 13], [7, 8, 14]]

What I've tried

I've tried using the zip function, but the results I get are nested:

list(zip(list_1,list_2))
# [((1, 2), 11), ((3, 4), 12), ((5, 6), 13), ((7, 8), 14)]

Notice how it doesn't follow the actual desired output because of the extra degree of nesting.

Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
Felipe D.
  • 1,157
  • 9
  • 19
  • 1
    You have a good start. Now iterate over the result of `zip()` to combine each tuple into a single list. – Code-Apprentice May 01 '23 at 22:43
  • Thanks for the tip!!! I actually was able to solve the problem while writing this question. In the end, I just posted my own answer below simultaneously with the original question. – Felipe D. May 01 '23 at 22:47
  • 1
    lol, I didn't even look at the author of the answer. Glad you found a solution. – Code-Apprentice May 01 '23 at 22:49

4 Answers4

3
zip(*zip(*list_1), list_2)

Explained: there is a canonical way to transpose a list, zip(*lst). All we're doing here is transposing, zipping, and transposing again

Marat
  • 15,215
  • 2
  • 39
  • 48
  • Awesome, thank you! This is even quicker than the list comprehension version!!! Btw, I think you've got an extraneous closing parehtesis at the end. Or a missing `list(` at the beginning =) – Felipe D. May 01 '23 at 22:48
  • 1
    @FelipeD. you're right, fixed – Marat May 01 '23 at 23:11
  • Using the `%timeit` magic command, this solution seems to take about 756ns on my PC. – Felipe D. May 01 '23 at 23:45
1

You can use list comprehension combined with a zip:

desired_output = [[*x, y] for x,y in zip(list_1,list_2)]

print(desired_output)
# [[1, 2, 11], [3, 4, 12], [5, 6, 13], [7, 8, 14]]

Note that *x unpacks the items of list_1, y just takes each individual item from list_2 and [*x, y] packs them back into one single list. Finally, the list comprehension around all of that takes care of iterating over everything and generating the full list.

PS: I'm answering my own question because I was able to figure out the solution on my own while writing the original question. I found hints to the solution in this other thread. But since that other post deals with a slightly different problem, I thought it would be beneficial to write a separate post here dedicated to this specific problem.

Felipe D.
  • 1,157
  • 9
  • 19
1

You need to add a list and a value for each pairing produced by zip. this requires converting the individual value to a small list (or some other means of creating a merged list):

[s1+[i2] for s1,i2 in zip(list_1,list_2)]
Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • This is awesome!!! I think this is the quickest out of all the answers so far. One small wrinkle though: it's not generalizable to cases where we're working with `tuples` and `tuples of tuples` (instead of `lists` and `lists` of `lists` as stated in the prompt). This wasn't mentioned anywhere in the original question, so it's impossible for you to have known I was looking for that too. Anyways, thanks for the lightning-fast answer! – Felipe D. May 01 '23 at 23:42
  • Using the `%timeit` magic command, this solution seems to take about 723ns on my PC. – Felipe D. May 01 '23 at 23:44
-1

I might be missing something but can't you just use a .append function?

list_1[0].append(list_2[0])
  • Hi @computer_goblin! Thanks for the answer!!! No, you're not missing anything - you're right, we can definitely use the `append` function. But the solution you provided only appends stuff to the first item. I'm looking for a solution that does that to all the items in the two lists and doesn't need to rely on a `for` loop. – Felipe D. May 01 '23 at 23:30
  • Understandable have a great day – computer_goblin May 02 '23 at 00:14