0

Consider the following for-loop:

def function(list, x, y):
   list[x] = y
   print(list)

list = [1,2,3]

for i in range(3):
   copyOfList = list
   function(copyOfList, i, 100)

I'm expecting the following output:

> [100, 2, 3]
> [1, 100, 3]
> [1, 2, 100]

Why?

Everytime before I run function, I do reinitialize copyOfList like this:

copyOfList = list

But Python gives me the following output:

> [100, 2, 3]
> [100, 100, 3]
> [100, 100, 100]

How can I achieve the desired output, and what is the reason behind Pythons output?

ozturkdk
  • 27
  • 4

0 Answers0