1

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

oh whoops, that was a typo, I meant L[0]. And thank you! I didn't know that it changed it, I thought the result meant that it didn't successfully extend it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.