Python List copy()

Python list‘s copy() function method is used to create shallow copy of the list.


Syntax

Here list1 is object of the list.


Return

It returns shallow copy of the list. Changes to new list does not impact old list.


Example

Let’s see a very simple example:

listOfCourses= ['Java','Python','C','C#'] listOfCoursesCopy=listOfCourses.copy() listOfCoursesCopy.append('PHP') print("listOfVehicles original list:",listOfCourses) print("listOfVehicles copied list:",listOfCoursesCopy)

Output:

listOfVehicles original list: [‘Java’, ‘Python’, ‘C’, ‘C#’]
listOfVehicles copied list: [‘Java’, ‘Python’, ‘C’, ‘C#’, ‘PHP’]

As you can see if we modify copied list, original list won’t be modified.

That’s all about Python List’s copy() method.

Leave a Reply

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