0

d={'sym':'A'}

leg = [d for i in range(2)]

for i in range(2):

leg[i]['sym'] = leg[i]['sym']+str(i)

print(leg)

outut: [{'sym': 'A01'}, {'sym': 'A01'}]

Expected output: [{'sym': 'A0'}, {'sym': 'A1'}]

  • Use `leg = [d.copy() for i in range(2)]` – mozway Jan 05 '23 at 05:59
  • The expression `leg = [d for i in range(2)]` created a list with two _copies_ of `d` in a list that was assigned to `leg`. This is identical to `[d] * 2` which the linked thread addresses. Try instead `leg = [{'sym':'A'} for i in range(2)]` which is one of the solutions provided in the thread. – metatoaster Jan 05 '23 at 06:03
  • @metatoaster In the actual case, d is quite lengthy. Is there any way to define d(the expression) separately? – PUJA DUTTA Jan 05 '23 at 07:04
  • You may instead use the alternative `d.copy()` or `deepcopy` as prescribed in one of the answers in the linked thread. – metatoaster Jan 05 '23 at 08:44

0 Answers0