1

I'm still pretty new at programming. I have a two Lists:

List1=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]
List2=['a','b','c']

and I want to put the values of one list into the other one, so my output looks like this:

List1=[[1,2,3,4,'a'],[5,6,7,8,'b'],[9,10,11,12,'c']]

So far I managed to write this:

for i in range(0,len(List1)):
    for row in List1:
        row.insert(5, List2[i])

But i get this:

List1=[[1,2,3,4,'a','b','c'],[5,6,7,8,'a','b','c'],[9,10,11,12,'a','b','c']]

I'm sure it's probably a simple mistake, but I can't find out what it is.

2
  • for row in list:, what is list? also, you're missing a closing ). Commented Dec 6, 2019 at 18:55
  • Sorry list is List1, i'll edit it! Commented Dec 6, 2019 at 19:00

5 Answers 5

2

Your posted code very explicitly adds every letter to each list. Instead, you want to add only the corresponding letter. You need one loop index for both lists, not nested loops.

for i in range(len(List1)):
    List1[i].append(List2[i])
Sign up to request clarification or add additional context in comments.

5 Comments

what a coincidence :o
I typed faster! :-)
Well I checked in python interpreter before comment :)
Thank you both for your answer! What if I want to choose the index where to insert List2?
Then you revert to insert, as in your original code.
2

You can use zip for that:

List1=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]
List2=['a','b','c']

List1 = [ a + list(b) for a, b in zip(List1, List2)]
print(List1)

Out:

[[1, 2, 3, 4, 'a'], [5, 6, 7, 8, 'b'], [9, 10, 11, 12, 'c']]

Please be aware, that this creates a new list and does not change the old one. If lists mutability is used this code will not work!

3 Comments

This creates a third list, though (List3) instead of modifying List1, as asked by OP.
Depending, if OP is working with the list as a mutable object it makes a difference, you are right, changed code, so that List1 is now there instead of List3, but might still cause problems, if list is supposed to be changed in function. Added info to answer, thanks
It might also be a problem in terms of performance as all the items of List1 are copied to a temporary value (expressed by your list comprehension) before the "old" list that List1 used to refer to is freed, in order to allow the assignment. This means that the values 1, 2, ... are unnecessarily copied.
1

You can use append to just add the ith indexed element to each element of List1

for i in range(len(List1)):
    List1[i].append(List2[i])

Comments

0

What you're looking for is zip:

for row, char in zip(List1, List2):
    row.append(char)

which gives:

>>> List1
[[1, 2, 3, 4, 'a'], [5, 6, 7, 8, 'b'], [9, 10, 11, 12, 'c']]

Comments

0

You can use an iterator:

it = iter(List2)
for lst in List1:
    lst.append(next(it))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.