How to Copy Elements from One List to Another in Python [6 Methods]

Do you want to copy a list in Python? In this Python tutorial, I will explain how to copy elements from one list to another in Python using different methods with some illustrative examples.

To copy elements from one list to another in Python, several methods are available: use copy() for a shallow copy, list slicing (original_list[:]) for a quick duplicate, list() function for an easy copy, copy.copy() for a generic shallow copy, copy.deepcopy() for a deep copy of nested structures, or the * operator ([*original_list]) for a concise and readable approach.

To Copy Elements from One List to Another in Python Methods

There are six different methods to copy elements from one list to another in Python:

  1. Using copy() function
  2. Using List slicing
  3. list() constructor
  4. copy.copy() method
  5. copy.deepcopy() method
  6. * (unpacking) operator

Let’s see them one by one using some demonstrative examples:

1. How to copy elements from one list to another in Python using copy() function

The copy() method is the most straightforward way to copy elements from one list to another in Python. It creates a shallow copy of the list.

Note: This method only copies the list itself, not the objects referenced by the list. If the original list contains objects and one of the objects is modified, the change will be reflected in both lists.

original_cities = ["New York", "Los Angeles", "Chicago"]
itinerary_cities = original_cities.copy()
print(itinerary_cities)

Output:

['New York', 'Los Angeles', 'Chicago']

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

How to copy elements from one list to another in Python

2. How to copy a list in Python using list slicing

List slicing in Python can also be used for copying lists. By slicing the entire list, we create a new list. Like the copy() method, list slicing creates a shallow copy.

presidents = ["Washington", "Lincoln", "Jefferson"]
backup_presidents = presidents[:]
print(backup_presidents)

Output:

['Washington', 'Lincoln', 'Jefferson']

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

Python copy elements from one list to another

3. Copy elements from one list to another in Python using list() constructor

Another way to copy a list is by using the built-in list() function, which creates a new list based on the elements of the existing one. This method, too, produces a shallow copy of the original list.

fast_food_chains = ["McDonald's", "Burger King", "Taco Bell"]
study_list = list(fast_food_chains)
print(study_list)

Output:

["McDonald's", 'Burger King', 'Taco Bell']

After executing the code in Pycharm, one can see the output in the below screenshot.

How to copy a list in python

4. Python copy elements from one list to another using copy.copy() method

Python has a copy module that provides a copy() function. This method is particularly useful when dealing with complex data structures. The copy.copy() creates a shallow copy of the list.

import copy
nba_teams = ["Lakers", "Bulls", "Celtics"]
analysis_teams = copy.copy(nba_teams)
print(analysis_teams)

Output:

['Lakers', 'Bulls', 'Celtics']

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

copy an item from one list to another in Python

5. Python copy elements from one list to another using copy.deepcopy() method

The deepcopy() method from the copy module creates a deep copy of the list, meaning that it also copies the inner objects. Modifications to objects in the original list will not affect the copied list.

import copy
states_cities = [["California", ["Los Angeles", "San Francisco"]], ["Texas", ["Houston", "Dallas"]]]
copied_states_cities = copy.deepcopy(states_cities)
print(copied_states_cities)

Output:

[['California', ['Los Angeles', 'San Francisco']], ['Texas', ['Houston', 'Dallas']]]

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

move elements from one list to another in Python

6. Copying a List to another List in Python using the * (unpacking) operator

The unpacking operator * can be used to copy lists. This method is similar to list slicing and is useful in contexts where we need to unpack the elements of the list. This approach also creates a shallow copy of the list.

us_holidays = ["Independence Day", "Thanksgiving", "Christmas"]
calendar_holidays = [*us_holidays]
print(calendar_holidays)

Output:

['Independence Day', 'Thanksgiving', 'Christmas']

After executing the code in Pycharm, one can see the output in the below screenshot.

Python program to copy all elements of one

Conclusion

Here, I have explained how to copy elements from one list to another in Python using six different methods copy() function, list slicing method, list() constructor, copy.copy() function, copy.deepcopy() function, and * operator with some demonstrative examples.

The selection of the method depends upon the requirement of the problem.

You may also like to read: