Menu Close

How to deep copy a list with Python?

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.

Posted in Python, Python Answers