What is the List.copy() method in Python?

Overview

The List.copy() method is one of the many list methods in Python. The List.copy() method is simply used to get the copy of a list.

Syntax

List.copy()

Parameters

List.copy() does not take any parameters.

Example

Let’s create a list of even numbers up to 10. We use List.copy() to get a copy of the list we create.

even_numbers = [2, 4, 6, 8, 10]
# now let us have a copy of our specified list in another list
even_numbers2 = even_numbers.copy()
print(even_numbers2)

Explanation

  • Line 1: We create a list of items and call it even_numbers.
  • Line 4: We make a copy of the items of our initial list, which we call even_numbers2.
  • Line 5: We print the new list, even_numbers2, which contains the copied items from the original list, even_numbers .

Note: Whenever you copy the items of a list to a new list, they become entirely independent. That is, any modification done to either of the two lists has nothing to do with the other.

Free Resources