Use Copy Method
In Python, you can use the built-in copy() method to create a shallow copy of a list. This means that the new list will contain the same elements, but it will be a different object in memory.
original_list = [1, 2, 3, 4]
copied_list = original_list.copy()
print("Original List:", original_list)
print("Copied List:", copied_list)
Do it
Use List Method
Another way to make a copy of a list in Python is by using the built-in list() function. This method takes an existing list as an argument and creates a new list with the same elements. Like the copy() method, using list() creates a shallow copy, meaning it duplicates the outer list but not any nested objects inside it. This approach is useful when you want a quick and simple way to duplicate a list without modifying the original.
original_list = [10, 20, 30, 40]
copied_list = list(original_list)
print("Original List:", original_list)
print("Copied List:", copied_list)
Do it
Use the slice Operator
Another simple and efficient way to copy a list in Python is by using the slice operator [:]. This creates a shallow copy of the original list by slicing from the beginning to the end. It’s a quick method that works well for duplicating lists with basic data types. However, like other shallow copy methods, it doesn’t create copies of nested objects within the list.
original_list = ['apple', 'banana', 'cherry']
copied_list = original_list[:]
print("Original List:", original_list)
print("Copied List:", copied_list)