$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #95

Which fix removes the bug?

import copy
a = {'x': [1, 2]}
b = copy.copy(a)
b['x'].append(3)
print(len(a['x']))
  1. Use copy.deepcopy(a) for the copy
  2. Use copy.clone(a) for the copy
  3. Change b['x'] to b.get('x') here
  4. Append with b['x'] = b['x'] + [3]
A shallow copy shares the inner list, so appending is visible through a.
Play Problem Solving ▸