Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
def main(): L=[] list1=[5,1,3] list2=[4,6,2] L.append(list1) L.append(list2) f(L) def f(L): for i in range(6) print L[i] IndexError: list index out of range
You're just appending the lists onto L so you get something like [[5, 1, 3], [4, 6, 2]]. You need to use extend like so:
[[5, 1, 3], [4, 6, 2]]
extend
L.extend(list1) L.extend(list2) print L # [5, 1, 3, 4, 6, 2]
Add a comment
Appending two items to an empty list makes a 2-element list. Perhaps you wanted L.extend() instead?
L.extend()
Required, but never shown
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.
Explore related questions
See similar questions with these tags.