Copying Python List

In this article, I have covered different ways to copy lists in python.
1. Assignment Operation
We can copy the list using assignment operation =
copied_list=original_list
Both original_list and copied_list will point to the same list object.
https://gist.github.com/IndhumathyChelliah/b55c82c8f02a19f9e9fa93877044e257

Modifications done in original_list will be reflected in copied_list and vice versa.
https://gist.github.com/IndhumathyChelliah/bf88c75da8e955ee3516da9b0cdee6c3

https://gist.github.com/IndhumathyChelliah/642a11b4649ec0775af8eac3eea9fa38
2. Using copy() function
We can copy the python list using the copy() function also. Both original_list and copied_list point to different list objects in memory.
https://gist.github.com/IndhumathyChelliah/d98160530b7ddb35778e1984dc94e888

Modifications done in the original list will not be reflected in the copied list and vice-versa.
https://gist.github.com/IndhumathyChelliah/c8818e22d02d1ba6fe799f18afcb9766

But if we modify the elements in the nested list, it will be reflected in both original_list and copied list.
https://gist.github.com/IndhumathyChelliah/a9c0e685fb78b1876aa2bf74fda5803f
3. Using list() constructor
We can copy the list using the list() constructor. Both original_list and copied list points to different list objects
copied_list=list(original_list)
https://gist.github.com/IndhumathyChelliah/2bee72a448063f8754ce74c18b014758
Modifying the elements in the original_list won’t be reflected in the copied_list and vice versa.
https://gist.github.com/IndhumathyChelliah/de8b89b2fe99bb937164186f1a828293
4. Indexing
We can copy the list using the indexing method.
s[i:j:k] — slice of s from i to j with step k
i → start index, j → end index, k → step
If we mention original_list[:], it will slice original_list from start to end with step 1.
It returns a copy of the list.
copied_list=original_list[:]
https://gist.github.com/IndhumathyChelliah/d5c62d16ba0a6718ee8c83856118bc07
Modifying orignial_list will not be reflected in copied_list and vice versa.
https://gist.github.com/IndhumathyChelliah/43892ed1792d89c969ef6606944c507a
How to copy the elements in reverse order using indexing?
original_list[::-1]
step=-1 means it starts from the last index to start_index.
https://gist.github.com/IndhumathyChelliah/10df96d86556392ecabb7bdd37e092da
How to copy alternate elements from a list?
https://gist.github.com/IndhumathyChelliah/6a8c45ed6a4da274bc02763056201f04
5. List Comprehension
We can copy the elements from the list using list comprehension.
copied_list=[i for i in original_list]
https://gist.github.com/IndhumathyChelliah/f2e631323bdd6ef48efe5ae49b35e303
Modifying the original_list won’t be reflected in the copied_list and vice_versa.
https://gist.github.com/IndhumathyChelliah/289abe24490e509c779d4fa854d9ed0e
And also we can apply any function to each element in the list and copy it using list comprehension.
Example: Square the elements in the list and copy them.
copied_list=[i*i for i in original_list]
Conclusion:
In this article, I have covered different ways to copy lists in python.
- Assignment operation → Both lists will point to the same list object
- Indexing, list(),list comprehension,copy() → Both lists(original and copied list) will point to different list-objects.
If you like to read more of my tutorials on Python and Data Science,
follow me on medium, Twitter
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee