Reputation: 27
I am trying to nest multiple lists in a single 'master' list, when ever i go through the lists to add the items in other lists to the master in order, so i can create a save file using pickle in another piece of code (not related to this problem at all),
I have not been able to find an alternative
a = [123456789]
b = [2, 6, "CF"]
c=["Helo", 4567]
d=[3,5,6,4,4,3,5]
e=["345sadf fg", 48736541546]
master = []
for i in range(5):
master.append([])
#insert items into list - Format = homework, tnotes, pnotes, camau, studentname
for a in range(len(a)):
master[0].append(a[a])
for b in range(len(b)):
master[1].append(b[b])
for c in range(len(c)):
master[2].append(c[c])
for d in range(len(d)):
master[3].append(d[d])
for e in range(len(e)):
master[4].append(e[e])
print(str(master))
I would expect:
[[123456789],
[2,6, "CF"],
["Helo",4657],
[3,5,6,4,4,3,5],
["345sadf fg",48736541546]]
Upvotes: 0
Views: 46
Reputation: 44838
The a
in for a in range(len(a)):
shadows the name a = [123456789]
from outer scope. So, when you do master[0].append(a[a])
, both a
s refer to the integer a
you got from range
. The same thing happens in all the other loops.
So, a[a]
(side note: this is highly confusing to begin with because it's unclear what a
this refers to; Python establishes strict rules concerning this) attempts to index the int
eger a
with the index a
, which makes no sense because "int
objects are not subscriptable", so you get an error.
You should name the index variables of your loops differently.
Upvotes: 4