Categories
Python Answers

How to deep copy a list with Python?

Spread the love

Sometimes, we want to deep copy a list with Python.

In this article, we’ll look at how to deep copy a list with Python.

How to deep copy a list with Python?

To deep copy a list with Python, we can use the copy.deepcopy method.

For instance, we write

import copy

a = [[1, 2, 3], [4, 5, 6]]
b = copy.deepcopy(a)

to call copy.deepcopy with a to return a deep copy of a and assign it to b.

Conclusion

To deep copy a list with Python, we can use the copy.deepcopy method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *