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 Laurent Lyaudet for List changes unexpectedly after assignment. How do I clone or copy it to prevent this?

$
0
0

There is another way of copying a list that was not listed until now : adding an empty list : l2 = l + [].I tested it with Python 3.8 :

l = [1,2,3]l2 = l + []print(l,l2)l[0] = 'a'print(l,l2)

Not the best answer but it works.


Viewing all articles
Browse latest Browse all 117

Trending Articles



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