Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

How to copy a list in Python

Sarah Tanveer

Lists are a commonly used data structure in Python. When you use lists, there may be a situation where you need to copy or clone a list. There are several methods we can use, depending on if we need a shallow or deep copy.

1. Use copy()

If you need a shallow copy of a list, the built-in copy() function can be used.

list.copy()

This function takes no parameters and returns a shallow copy of the list. This means any changes made to the copied list will not impact the original list.

# delcaring a list
originalList = [1, 2, 3, 4]

# creating a shallow copy using function
shallowCopy = originalList.copy()
print("Shallow copy: ", shallowCopy)

# adding an element to the shallow copy
shallowCopy.append(5)

# printing the shallow copy after adding an element
print("Shallow copy after appending an element: ", shallowCopy)

# printing the original list to show no changes took place
print("Original copy remains the same: ", originalList)

2. Use slicing

List slicing can be used to easily make a copy of a list. This method is called cloning. The original list will remain unchanged.

In this method, slicing is used to copy each element of the original list into the new list.

# delcaring a list
originalList = [1, 2, 3, 4]

# creating a clone using slicing
cloneList = originalList[:]
print("Clone of list: ", cloneList)

# adding an element to shallow clone
cloneList.append(5)

# printing the cloned list after adding an element
print("Clone of list after appending element: ", cloneList)

# printing the original list to show no changes took place
print("Original copy remains the same: ", originalList)

3. Use a for loop and append()

In this method, a for loop traverses through the elements of the original list and adds them one by one to the copy of the list, through append(). The built-in append() function takes a value as an argument and adds it to the end of a list.

list.append(val)

This creates a clone of the original list, so the original list is not changed.

# delcaring a list:
originalList = [1, 2, 3, 4]

# declaring an empty list
cloneList = []

# creating a clone using a loop
for i in originalList:
    cloneList.append(i)

print("Clone of list: ", cloneList)

# adding an element to the shallow clone
cloneList.append(5)

# printing the cloned list after adding element
print("Clone of list after appending element: ", cloneList)

# printing the original list to show no changes took place
print("Original copy remains the same: ", originalList)

4. Use the assignment operator

When an assignment operator is used, a new list object will not be created. Instead, it will cause two variables to point to the same place in memory. This will result in a deep copy, i.e., changing the copy will change the original.

import copy
# delcaring a list
originalList = [1, 2, 3, 4]

# creating a deep copy using slicing
deepCopy = originalList
print("Clone of list: ", deepCopy)

# adding an element to shallow deep copy
deepCopy.append(5)

# printing the deep copy list after adding element
print("Clone of list after appending element: ", deepCopy)

# printing the original list to show it changed too
print("Original copy remains the same: ", originalList)

RELATED TAGS

python
communitycreator
RELATED COURSES

View all Courses

Keep Exploring