Copying Lists in Python

You can copy a list using the copy() method or by using list slicing.

Using copy() Method

Example: Copying a List

# Using copy()
original_list = ["Apple", "Banana", "Cherry"]
copied_list = original_list.copy()
print("Original List:", original_list)
print("Copied List:", copied_list)

Output

Original List: ['Apple', 'Banana', 'Cherry']

Copied List: ['Apple', 'Banana', 'Cherry']

Explanation: The copy() method creates a shallow copy of the list.

Using List Slicing

Example: Copying a List with Slicing

# Using list slicing
copied_list = original_list[:]
print("Copied List:", copied_list)

Output

Copied List: ['Apple', 'Banana', 'Cherry']

Explanation: List slicing [:] creates a shallow copy of the list.