How Python Copy List? 5 Methods With Example Program

What is Python List copy()?

The Python copy() list method returns a shallow copy of a list.

It copies the list and returns the copied list.

copy() List Python Syntax

The syntax of copy() list in Python is:

list_new = list.copy()

copy() Parameters

The copy() method does not take any parameter.

copy() Return Value

The copy() method returns a new list. It doesn’t modify the original list.

Example Program:

# mixed list
prime_numbers = [2, 3, 5]


numbers = prime_numbers.copy()


print('Copied List:', numbers)

Output:

Copied List: [2, 3, 5]

How to copy a list in Python?

In this section, we will explore different methods to copy a list in Python, using the target list: original_list = [1, 2, 3, 4, 5].

Pay close attention to the subtle distinctions among the techniques discussed below:

Method 1: Using the copy() Method

Python lists have a built-in copy() method that allows you to create a shallow copy of the list.

A shallow copy means the new list will contain references to the same objects as the original list, rather than creating new instances of the objects themselves.

The syntax for this is:

new_list = original_list.copy()

Let’s apply the copy() method:

original_list = [1, 2, 3, 4, 5]
new_list = original_list.copy()
print(new_list)

Output:

[1, 2, 3, 4, 5]

The copy() method is useful when you want a separate list but don’t need deep copies of elements.

Method 2: Using Slicing

One of the simplest ways to copy a list in Python is by using slicing.

It involves taking a slice of the entire list, which creates a new list with all the elements from the original.

The syntax for slicing is as follows:

new_list = original_list[:]

The colon : without any start or end index specifies that the slice should cover the entire list. Let’s see this method in action:

original_list = [1, 2, 3, 4, 5]
new_list = original_list[:]
print(new_list)

Output:

[1, 2, 3, 4, 5]

Slicing is a popular and efficient way to copy lists, especially when you want to create an independent copy.

Method 3: Using the list() Function

Another approach to copying a list in Python is by using the list() function. This function takes an iterable (like a list) as an argument and creates a new list with the same elements.

Here’s how to use it:

new_list = list(original_list)

Let’s try this method:

original_list = [1, 2, 3, 4, 5]
new_list = list(original_list)
print(new_list)

Output:

[1, 2, 3, 4, 5]

The list() function can be handy when you need to duplicate a list quickly, and it works well for most scenarios.

Method 4: Using the copy.deepcopy() Function

For more complex scenarios, where the list contains nested objects, the copy module in Python provides a powerful deepcopy() function.

Unlike a shallow copy, a deep copy creates entirely independent copies of all nested objects, ensuring that the new list remains fully independent.

To use deepcopy():

import copy

new_list = copy.deepcopy(original_list)

Let’s see an example:

import copy

original_list = [1, [2, 3], 4, 5]
new_list = copy.deepcopy(original_list)
print(new_list)

Output:

[1, [2, 3], 4, 5]

copy.deepcopy() is ideal for lists with nested structures, ensuring that any modifications in the copied list do not affect the original.

Method 5: Using List Comprehension

List comprehension is a concise and elegant way to create a new list based on an existing list’s elements.

To copy a list using list comprehension:

new_list = [x for x in original_list]

Let’s apply list comprehension:

original_list = [1, 2, 3, 4, 5]
new_list = [x for x in original_list]
print(new_list)

Output:

[1, 2, 3, 4, 5]

List comprehension allows you to copy lists while performing additional operations on the elements if needed.

Method 6: Using the copy() Function from the copy Module

The copy module also provides a copy() function, which behaves similarly to the list() function.

This method offers an alternative way to duplicate lists:

import copy

new_list = copy.copy(original_list)

Let’s see this method in action:

import copy

original_list = [1, 2, 3, 4, 5]
new_list = copy.copy(original_list)
print(new_list)

Output:

[1, 2, 3, 4, 5]

The copy() function from the copy module is comparable to the list() function and can be used interchangeably for most cases.

Copy List Using =

An additional approach to copy a list is by utilizing the “=” operator. However, the sole drawback of this method is that it does not result in a shallow copy.

#Defining a list
list = ['a','b','c']



list_new = list


print ("This is the new list: " + str(list_new))


list_new.append('d')


print ("The new list after adding a new element: " + str(list_new))
print ("The old list after adding a new element" + str(list))

In this scenario, we generated a list and proceeded to make a duplicate of it. However, when attempting to append an element to the new list, it becomes evident that the original list is altered. This is due to the fact that this approach does not produce a shallow copy.

Program Output:

This is the new list: ['a', 'b', 'c']
The new list after adding a new element: ['a', 'b', 'c', 'd']
The old list after adding a new element['a', 'b', 'c', 'd']

Copy List Using Slicing Syntax

This represents an alternative method for duplicating a list, employing the technique of slicing.

Example Program:

#Defining a list
list = ['a','b','c']


#Copying list using slicing
list_new = list[:]


print ("This is the new list: " + str(list_new))


list_new.append('d')


print ("The new list after adding a new element: " + str(list_new))
print ("The old list after adding a new element" + str(list))

Here, we created a list and then copied it. When you try to add an element to the new list, you can see the old list is modified as this method creates shallow copy using the slicing syntax.

Program Output:

This is the new list: ['a', 'b', 'c']
The new list after adding a new element: ['a', 'b', 'c', 'd']
The old list after adding a new element['a', 'b', 'c']

Conclusion

The Copy List method is an easy way to copy. This is a built-in method that can make writing code easier.

Also, you can use the other method discussed above, but make sure you know about shallow copy.

I hope this simple Python tutorial can help you copy a list.

Leave a Comment