1

When I try to modify a value in a list, it is also modifying a separate value in another list. I don't believe this would be a bug, but I would like to know how to treat these variables independently.

I have tried this for single variables instead of lists, and this problem does not occur. I could set the lists separately, but that seems unnecessary for when there are a large number of lists.

numbers = [1,2,3,4,5]
list_A = numbers
list_B = numbers

print("list A:",list_A)
print("list B:",list_B)
list_A[2] = 10
print("list A:",list_A)
print("list B:",list_B)

I would expect an output of:
list_A:[1, 2, 3, 4, 5]
list_B:[1, 2, 3, 4, 5]
list_A: [1, 2, 10, 4, 5]
list_B: [1, 2, 3, 4, 5]

but instead get this:
list_A:[1, 2, 3, 4, 5]
list_B: [1, 2, 3, 4, 5]
list_A: [1, 2, 10, 4, 5]
list_B: [1, 2, 10, 4, 5]

where both lists have been modified

1

5 Answers 5

3
numbers = [1,2,3,4,5]
list_A = numbers.copy()
list_B = numbers.copy()

print("list A:",list_A)
print("list B:",list_B)
list_A[2] = 10
print("list A:",list_A)
print("list B:",list_B)

this is python so you have to do this.

for further information see How to clone or copy a list?

Sign up to request clarification or add additional context in comments.

1 Comment

2

As you do here:

list_A = numbers
list_B = numbers

You're copying the whole object, however it will change all the lists, so you would need:

list_A = numbers.copy()
list_B = numbers.copy()

Or:

list_A = numbers[:]
list_B = numbers[:]

Or:

list_A = list(numbers)
list_B = list(numbers)

1 Comment

I must have misclicked earlier, sorry. Thanks for the help :)
1

Creating a list with newList = oldList means the two lists are linked, as they're both references to the same list. Use newList = list(oldList) to create a new list.

Comments

1

The reason you are seeing this is because Python is assigning the reference and not the value for structures by default. To achieve what you want you need to use a way of copying like shown in the reply above.

You will see this if you try to print the variables

Comments

0

You are accessing the array using the reference. You have multiple options to do that. The simplest one would be make the array again by iterating:

numbers = [1,2,3,4,5]
list_A = [i for i in numbers]
list_B = [i for i in numbers]

With this you are creating array with different reference.

Also you can read this document: https://docs.python.org/2/library/copy.html

You can try these too:

# shallow copy of array, means that it handles the simple copy but not the nested ones(if you have a `list` in `list`)
list_A = copy.copy(numbers)
list_B = copy.copy(numbers)

# deep copy of array, so handles nested ones too
list_A = copy.deepcopy(numbers)
list_B = copy.deepcopy(numbers)

In your situation both of them work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.