Reputation: 75
I'm having trouble adding numbers to a list that is already in a list. For example, say I'm given:
L = [[1, 2, 3], [100, 101, 102]]
I'm trying to append to L[1]
to get:
L = [[1, 2, 3, 4, 5, 6], [100, 101, 102]]
The way I was going about this was L[1].extend([4, 5, 6])
but was getting the result None
.
Any help would be appreciated.
Upvotes: 1
Views: 371
Reputation: 282026
First, that's L[0]
you want to append to, not L[1]
. Indices start at 0.
Second, L[0].extend([4, 5, 6])
will work fine. It modifies the list and returns None
. Demo:
>>> L = [[1, 2, 3], [100, 101, 102]]
>>> L[0].extend([4, 5, 6])
>>> L
[[1, 2, 3, 4, 5, 6], [100, 101, 102]]
Just don't try to do anything with extend
's return value, and you should be fine.
Upvotes: 3