Quantcast
Channel: How do I clone a list so that it doesn't change unexpectedly after assignment? - Stack Overflow
Viewing all articles
Browse latest Browse all 117

Answer by Salindaw for How do I clone a list so that it doesn't change unexpectedly after assignment?

$
0
0
new_list = my_list

because: new_list will only be a reference to my_list, and changes made in new_list will automatically also be made in my_list and vice versa

There are two easy ways to copy a list

new_list = my_list.copy()

or

new_list = list(my_list)

Viewing all articles
Browse latest Browse all 117

Trending Articles



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