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 Corman for How to clone or copy a list?

I wanted to post something a bit different then some of the other answers. Even though this is most likely not the most understandable, or fastest option, it provides a bit of an inside view of how...

View Article



Answer by Chris_Rands for How to clone or copy a list?

Note that there are some cases where if you have defined your own custom class and you want to keep the attributes then you should use copy.copy() or copy.deepcopy() rather than the alternatives, for...

View Article

Answer by SCB for How to clone or copy a list?

It surprises me that this hasn't been mentioned yet, so for the sake of completeness... You can perform list unpacking with the "splat operator": *, which will also copy elements of your list. old_list...

View Article

Image may be NSFW.
Clik here to view.

Answer by Aaditya Ura for How to clone or copy a list?

Let's start from the beginning and explorer it a little deep : So Suppose you have two list : list_1=['01','98'] list_2=[['01','98']] And we have to copy both list , now starting from the first list:...

View Article

Answer by jainashish for How to clone or copy a list?

A very simple approach independent of python version was missing in already given answers which you can use most of the time (at least I do): new_list = my_list * 1 #Solution 1 when you are not using...

View Article


Answer by Ravi Shankar for How to clone or copy a list?

new_list = my_list[:] new_list = my_list Try 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 How to clone or copy a list?

Python 3.6 Timings Here 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 How to clone or copy a list?

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 How to clone or copy a list?

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 How to clone or copy a list?

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 How to clone or copy a list?

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

View Article

Answer by cryo for How to clone or copy a list?

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

View Article

Answer by Felix Kling for How to clone or copy a list?

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 How to clone or copy a list?

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

View Article

Answer by Paul Tarjan for How to clone or copy a list?

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

View Article


How to clone or copy a list?

What are the options to clone or copy a list in Python? While using new_list = my_list, any modifications to new_list changes my_list everytime. Why is this?

View Article

Answer by B.Mr.W. for How to clone or copy a list?

A slight practical perspective to look into memory through id and gc. >>> b = a = ['hell', 'word'] >>> c = ['hell', 'word'] >>> id(a), id(b), id(c) (4424020872, 4424020872,...

View Article


Answer by Dr. Hippo for How to clone or copy a list?

Remember that in Python when you do: list1 = ['apples','bananas','pineapples'] list2 = list1List2 isn't storing the actual list, but a reference to list1. So when you do anything to list1, list2...

View Article

Answer by shahar_m for How to clone or copy a list?

The deepcopy option is the only method that works for me:from copy import deepcopya = [ [ list(range(1, 3)) for i in range(3) ] ]b =...

View Article

Answer by Roshin Raphel for How to clone or copy a list?

This is because, the line new_list = my_list assigns a new reference to the variable my_list which is new_listThis is similar to the C code given below,int my_list[] = [1,2,3,4];int *new_list;new_list...

View Article
Browsing all 116 articles
Browse latest View live




Latest Images