Make a Copy of a List in Python

  • Post category:List

Creating a copy of a list in Python can be done using the slicing technique or the copy() method. Here are two examples:

Example 1:

original_list = [1, 2, 3, 4, 5]
copied_list = original_list[:]
print(f"Copied list: {copied_list}")

Output:

Copied list: [1, 2, 3, 4, 5]

Example 2:

fruits = ['apple', 'banana', 'orange']
copied_fruits = fruits.copy()
print(f"Copied fruits: {copied_fruits}")

Output:

Copied fruits: [‘apple’, ‘banana’, ‘orange’]