+ 2
Why is un2 an empty array ?
I created what i believed was a simple code to sort an array "A test" but discovered my un2 was empty when I was going to do a "B test". I started by creating array 'un' and my second array was a copy of 'un' as 'un2' ``` un = [91,82,43,54,36,67,28,19,101,70] un2 = un # Create a copy of un print("us2:",un2) print("us:",un) na = [] for i in range(len(un)): x = min(un) na.append(x) un.remove(x) print("sa:",na) print("us2:",un2) #beginning of "B test" ``` What happened un2 is empty ?
15 ответов
+ 10
other ways of making shallow copy:
un2 = un[:]
un2 = list(un)
un2 = [*un]
for nested lists, you will need to use the copy module using copy.deepcopy()
+ 6
The following explanation can be seen in my code sample here:
https://www.sololearn.com/en/compiler-playground/cPOCEjMhk6tj
When you edit the values inside UN, those are the same values for UN2.
un and un2 are both pointing to the same data in memory. For example.
un = [1,2,3]
un2 = un
print(un)
print(un2)
Both are the same output.
Then try this:
un = [1,2,3]
un2 = un
un[0] = 4
print(un)
print(un2)
Both still output the same thing.
Now try this:
un = [1,2,3]
un2 = un
un = [4,5,6]
print(un)
print(un2)
These will output different results.
The reason why has to do with understanding there are two things going on with the variables. Each variable is technically a pointer. un and un2 are two variables, but point to the same data in memory. The data is the list [1,2,3]. This is in one location in memory but pointed to by two different pointers, un and un2.
If you create a new list [4,5,6] and point un to that, you end up with un = [4,5,6] and un2 = [1,2,3]
If you want un and un2 to have different copies of the data, you can do that like this:
un = [1,2,3]
un2 = un.copy()
un[0] = 4
print(un)
print(un2)
The .copy() will replicate the data into a new list so you can update un without affecting un2.
+ 6
BroFar
I'm not sure which method is better. For short lists, I guess it doesn't matter. I like the brevity of the slice method [:], but Vitaly Sokol's *un2, is shorter by one character 😎.
Jerry Hobby 's suggestion to use .copy() method is the most easily understandable and is probably the best choice for codes that's going to be shared with other people.
+ 5
SoloProg
lists being a reference type is sometimes useful. Mutating the items become more efficient. Just have to remember not to use = to duplicate them.
+ 4
un2 shares the same reference as un in memory – un2 is just another alias for the existing un.
+ 4
I learned a lot of languages.
So i confuse the syntax, so i sometimes need to research the correct code.
(⌐■_■)
+ 4
Hum ... just a follow-up on my question of hierarchy Bob_Li and thank you for explanation.
Choose the syntax based on your specific needs:
Creating a shallow copy of a list: Use [:] or .copy().
Converting an iterable to a list:
Use list(un).
Unpacking elements from an iterable:
Use [*un]
Then there is deepcopy() #
import copy ... copy.deepcopy(un)
Creates a deep copy, which recursively copies nested objects as well.
Use Case: When you need to copy objects with nested structures (e.g., nested lists, dictionaries) and avoid modifying the original object.