2
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

2 Answers 2

5

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:

L.extend(list1)
L.extend(list2)
print L      # [5, 1, 3, 4, 6, 2]
Sign up to request clarification or add additional context in comments.

Comments

2

Appending two items to an empty list makes a 2-element list. Perhaps you wanted L.extend() instead?

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.