Python List copy()

In this tutorial, we will see about Python List‘s copy method.Python List clear method is used to shallow copy the list.

Python List copy example

You can simply use copy method to shallow copy the list.It returns shallow copy of the list.
Let’s understand this with the help of simple example.

Output:

Original List: [1, 2, 3, 4] Copied list: [1, 2, 3, 4, 5]

As you can see change in copied list does not original list.
Let’s understand now, what is meant by shallow copy here.

Output:

Original List: [[‘one’, ‘two’, ‘three’], [‘four’, ‘five’]] Copied list: [[‘one’, ‘two’, ‘three’], [‘four’, ‘five’]]

As you can see here, change in inner list affected both the list.As when you use list’s copy method, it is actually shallow copy, so changes in inner list will impact both the list.
That’s all about Python List copy method.

Was this post helpful?

Leave a Reply

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