To pass only values that are not Python object addresses

Asked 2 years ago, Updated 2 years ago, 21 views

For example,

a = [1,2,3]
b = a
a.remove(1)
print(b)

When you do, [2, 3] is output. If you take the id(), the address is the same. I mean, the assignment operator '=' seems to hand over the address of the object. Is there a way to hand over only the value of a ([1,2,3]) by changing the address of b?

Simply put, we want the output of the above code to be [1,2,3]. Thank you.

python

2022-09-22 15:14

1 Answers

You can use one of the following instead of b = a.

b = a.copy()
b = a[:]
b = list(a)


2022-09-22 15:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.