Languages
[Edit]
EN

Python - copy list

0 points
Created by:
Kia-H
396

In this article, we would like to show you how to copy list in Python.

Quick solution:

my_list = ['A', 'B', 'C']

list_copy = my_list.copy()

print(list_copy)  # ['A', 'B', 'C']

 

1. Practical example using copy() method

In this example, we use copy() method to create a copy of my_list.

my_list = ['A', 'B', 'C']

list_copy = my_list.copy()

print(list_copy)  # ['A', 'B', 'C']

Output:

['A', 'B', 'C']

Note:

You can't copy the list by simply using list2 = list1 because list2 will only be a reference to list1.

2. Using list() constructor

In this example, we use list() constructor to create a copy of my_list.

my_list = ['A', 'B', 'C']

list_copy = list(my_list)

print(list_copy)  # ['A', 'B', 'C']

Output:

['A', 'B', 'C']

Python - list (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join