-3

I want to create a dictionary with {k:[list]} where k value of i for the first iteration, it works but {3:[20,20,20]} but when increase and added to the dictionary {3:[20,20,20,20],4:[20,20,20,20]} instead of {3:[20,20,20],4:[20,20,20,20]} and goin {3:[20,20,20,20,20],4:[20,20,20,20,20],5:[20,20,20,20,20]} instead of
{3:[20,20,20],4:[20,20,20,20],5:[20,20,20,20,20]}

d0 = 20
tempsq=[]
c=[]
anon={}
minm=0
for i in range(3,len(sq)):
    if i<2*kd:
        # print(i)
        cost=compute_I(sq[0:i])
        c.append(cost)
        #from hear
        tempsq.clear()
        for j in range(0,i):
             tempsq.append(d0)
        if not anon:
            anon = {i: tempsq}
            print(anon)
        else:
            anon.update({i:tempsq})
            print(anon)


print(anon)
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52

1 Answers1

0

From what I understand you want to create a list:

{3: [20,20,20], 4:[20,20,20,20],...}

You can achieve that running the following:

value = 20
min_iteration = 3
max_iteration = 5

{i : [value]*i for i in range(min_iteration, max_iteration+1)}

Which yields:

{3: [20, 20, 20], 4: [20, 20, 20, 20], 5: [20, 20, 20, 20, 20]}
Yoavhayun
  • 499
  • 1
  • 8
  • 16