# this works, but see below
# allTheLists = []
# for j in range(int(L)):
# allTheLists[j] = []
# more pythonic:
allTheLists = [[] for x in range(int(L))]
#...
allTheLists[j].append(jListItem)
printAllTheLiats[listIndex][itemIndex]
# etc
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
It is very difficult or error prone to do assignment to list elements so often I prefer to use dicts.
# elements of list are mutable and can be variable reference, not values
L='10'
allthelists = [[] for x in range(int(L))]
#...
j=5
newitem=['abc']
allthelists[j]=newitem ## wrong, or more dangerous
##allthelists[j]=newitem[:] ## right, or safer, copy by [:], newitem does not change
moreitem='def'
allthelists[j].append(moreitem) ## changes newitem if not copy
print 'New item',newitem
newitem='ghi' ## in this case no problem as newitem overwriten
print "allthelists:"
print allthelists ## element of allthelist did not change
prove to swap commenting in lines 7 and 8.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852