To copy a list in Python, you can use the list copy() method or the list slice syntax.
In this example,
original_list
with some elements.copy()
method of original_list
to create a copy named copied_list
.copied_list
contains all elements of original_list
.original_list = [1, 2, 3]
copied_list = original_list.copy()
print(f'Original List: {original_list}')
print(f'Copied List: {copied_list}')
Original List: [1, 2, 3] Copied List: [1, 2, 3]
In this example,
original_list
with some elements.[:]
to create a copy named copied_list
.copied_list
contains all elements of original_list
.original_list = [1, 2, 3]
copied_list = original_list[:]
print(f'Original List: {original_list}')
print(f'Copied List: {copied_list}')
Original List: [1, 2, 3] Copied List: [1, 2, 3]
In this tutorial, we learned How to Copy a List in Python language with well detailed examples.