Quantcast
Channel: How do I clone a list so that it doesn't change unexpectedly after assignment? - Stack Overflow
Browsing all 116 articles
Browse latest View live
↧

Answer by Ravi Shankar for List changes unexpectedly after assignment. Why is...

new_list = my_list[:]new_list = my_listTry to understand this. Let's say that my_list is in the heap memory at location X, i.e., my_list is pointing to the X. Now by assigning new_list = my_list you're...

View Article


Answer by River for List changes unexpectedly after assignment. Why is this...

Python 3.6 TimingsHere are the timing results using Python 3.6.8. Keep in mind these times are relative to one another, not absolute.I stuck to only doing shallow copies, and also added some new...

View Article


Answer by AMR for List changes unexpectedly after assignment. Why is this and...

All of the other contributors gave great answers, which work when you have a single dimension (leveled) list, however of the methods mentioned so far, only copy.deepcopy() works to clone/copy a list...

View Article

Answer by jack for List changes unexpectedly after assignment. Why is this...

There are many answers already that tell you how to make a proper copy, but none of them say why your original 'copy' failed. Python doesn't store values in variables; it binds names to objects. Your...

View Article

Answer by Aaron Hall for List changes unexpectedly after assignment. Why is...

What are the options to clone or copy a list in Python?In Python 3, a shallow copy can be made with:a_copy = a_list.copy()In Python 2 and 3, you can get a shallow copy with a full slice of the...

View Article


Answer by anatoly techtonik for List changes unexpectedly after assignment....

I've been told that Python 3.3+adds the list.copy() method, which should be as fast as slicing:newlist = old_list.copy()

View Article

Answer by cryo for List changes unexpectedly after assignment. Why is this...

Felix already provided an excellent answer, but I thought I'd do a speed comparison of the various methods:10.59 sec (105.9 µs/itn) - copy.deepcopy(old_list)10.16 sec (101.6 µs/itn) - pure Python...

View Article

Answer by Felix Kling for List changes unexpectedly after assignment. Why is...

With new_list = my_list, you don't actually have two lists. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the...

View Article


Answer by erisco for List changes unexpectedly after assignment. Why is this...

Python's idiom for doing this is newList = oldList[:]

View Article


Answer by Paul Tarjan for List changes unexpectedly after assignment. Why is...

Use thing[:]>>> a = [1,2]>>> b = a[:]>>> a += [3]>>> a[1, 2, 3]>>> b[1, 2]>>>

View Article

List changes unexpectedly after assignment. Why is this and how can I prevent...

While using new_list = my_list, any modifications to new_list changes my_list every time. Why is this, and how can I clone or copy the list to prevent it?

View Article

--- Article Not Found! ---

*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***

View Article

--- Article Not Found! ---

*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***

View Article


--- Article Not Found! ---

*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***

View Article

--- Article Not Found! ---

*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***

View Article


--- Article Not Found! ---

*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***

View Article

--- Article Not Found! ---

*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***

View Article


--- Article Not Found! ---

*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***

View Article

--- Article Not Found! ---

*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***

View Article

--- Article Not Found! ---

*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***

View Article
Browsing all 116 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>