-1

For example:

a = {}; 
b = [];
for i in range(20):
    b.append(i)
    a[i] = b

After loops, all keys of a is linked to the same final list b [0,1,2,...,19]. However, what I want is that a[0] = [0], a[1] = [0,1], a[2] = [0,1,2]. I have no idea what is wrong here.

ᴀʀᴍᴀɴ
  • 4,443
  • 8
  • 37
  • 57
Yiming Che
  • 23
  • 1
  • 6

1 Answers1

0
a = {}; 
b = [];
for i in range(20):
    b=[k for k in range(i)]
    a[i] = b

Hope it helps

TavoGLC
  • 889
  • 11
  • 14
  • 2
    `[k for k in range(i)]` should probably just be `list(range(i))`. Also, this doesn't really explain to the OP what is wrong, or what you answer is doing to fix it. – juanpa.arrivillaga Feb 21 '19 at 23:54