python index error: 2 lists that are inside a list
def main():
L=[]
list1=[5,1,3]
list2=[4,6,2]
L.append(lis开发者_C百科t1)
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:
L.extend(list1)
L.extend(list2)
print L # [5, 1, 3, 4, 6, 2]
Appending two items to an empty list makes a 2-element list. Perhaps you wanted L.extend()
instead?
精彩评论