More Examples on List copy() Method

Let us see a few examples of the list copy() method.

Example 1: Simple List Copy

In this example, we are creating a List of Python strings and we are using copy() method to copy the list to another variable.

Python3




lis = ['Geeks','for','Geeks']
new_list = lis.copy()
print('Copied List:', new_list)


Output

Copied List: ['Geeks', 'for', 'Geeks']

Example 2: Demonstrating the working of List copy() 

Here we will create a Python list and then create a shallow copy using the copy() function. Then we will append a value to the copied list to check if copying a list using copy() method affects the original list.

Python3




# Initializing list
lis1 = [ 1, 2, 3, 4 ]
 
# Using copy() to create a shallow copy
lis2 = lis1.copy()
 
# Printing new list
print ("The new list created is : " + str(lis2))
 
# Adding new element to new list
lis2.append(5)
 
# Printing lists after adding new element
# No change in old list
print ("The new list after adding new element : \
" + str(lis2))
print ("The old list after adding new element to new list  : \
" + str(lis1))


Output

The new list created is : [1, 2, 3, 4]
The new list after adding new element : [1, 2, 3, 4, 5]
The old list after adding new element to new list  : [1, 2, 3, 4]

Note: A shallow copy means if we modify any of the nested list elements, changes are reflected in both lists as they point to the same reference.

Python List copy() Method

The list Copy() method makes a new shallow copy of a list.

Example

Python3




# Using list fruits
fruits = ["mango","apple","strawberry"]
# creating a copy shakes
shakes = fruits.copy()
# printing shakes list
print(shakes)


Output

['mango', 'apple', 'strawberry']

Similar Reads

What is List Copy() Method?

...

List copy() Method Syntax

The list Copy() function in Python is used to create a copy of a list. There are two main ways to create a copy of the list Shallow copy and Deep copy. We will discuss the list copy() method in detail below....

How to Create a Simple Copy of a List in Python

list.copy()...

More Examples on List copy() Method

Copying and creating a new list can be done using copy() function in Python....

Shallow Copy and Deep Copy

...

Copy List Using Slicing

Let us see a few examples of the list copy() method....