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. Why is this and how can I prevent it?

$
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)

It is 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>