How to copy a list in Python?

Python Tutorials (31 Part Series)

1 How to use ‘not equal’ operator in Python?
2 How to use exponents in Python?
27 more parts…
3 How to check if file exists in Python?
4 How to represent an infinite number in Python?
5 Various ways to sum numbers in Python
6 Python – pass by reference
7 4 Ways to implement Python Switch Case Statement
8 How to create a list of lists in Python?
9 Python regex: How to search and replace strings
10 How to copy a list in Python?
11 Python rename file: How to rename a file in Python
12 How to calculate the length of an array in Python?
13 How to use Open() in Python?
14 How to sort dictionary by value in Python?
15 How to get Timestamp in Python?
16 How to block a comment in Python?
17 How does return() in Python work?
18 Python constructors – How to use them?
19 Python append to string – How is it done?
20 Python list contains: How to check if an item exists in list?
21 Python Initialize List – How to do it?
22 How to print a list in Python?
23 Python remove file – How to delete a file?
24 What are Identifiers in Python?
25 Python Reserved Words List – Your Complete Guide
26 Different Arithmetic operators in Python
27 Different Comparison operators in Python
28 Different Assignment operators in Python
29 What are the Control Statements in Python?
30 Looping Statements in Python
31 What is a while loop in Python?

In this blog, we will learn about a new method in Python – copy list. While writing code, there are times when a user needs to reuse an object. Re-typing every single line of code seems useless. Hence, we use the copy method available in the list.

Table of contents

  1. Introduction to copy list
  2. Python copy list method
  3. Copy list using =
  4. Using built-in method list() to copy list
  5. Closing thoughts

Introduction to copy list

Easy as it sounds, copying a list in Python is quite simple. But we still have to follow the syntax and know when and how to use the copy list method. This method makes it easy to write parts of code that occur multiple times.

Just to make it clear, this method only works on the list.

Python copy list method

Python copy() method copies the list and returns the copied list. It does not take any parameter and returns a shallow copy of the list.

A shallow copy is the one that does not show any modification on the original list. The copied list points to a different memory location than the original list, so changing one list does not affect another list.

Syntax:

list_new = list.copy()
list_new = list.copy()
list_new = list.copy()

Enter fullscreen mode Exit fullscreen mode

Input:

#Defining a list
list = [a,b,c]
#Copying list
list_new = list.copy()
print ("This is the new list: " + str(list_new))
list_new.append(d)
print ("The new list after adding a new element: " + str(list_new))
print ("The old list after adding a new element" + str(list))
#Defining a list
list = [a,b,c]

#Copying list
list_new = list.copy()

print ("This is the new list: " + str(list_new))

list_new.append(d)

print ("The new list after adding a new element: " + str(list_new))
print ("The old list after adding a new element" + str(list))
#Defining a list list = [a,b,c] #Copying list list_new = list.copy() print ("This is the new list: " + str(list_new)) list_new.append(d) print ("The new list after adding a new element: " + str(list_new)) print ("The old list after adding a new element" + str(list))

Enter fullscreen mode Exit fullscreen mode

Here, we created a list and then copied the list. When you add an element to the new list, you can see that the old list does not show the modification.

Output:

This is the new list: [a,b,c]
The new list after adding a new element: [a,b,c,d]
The old list after adding a new element: [a,b,c]
This is the new list: [a,b,c]
The new list after adding a new element: [a,b,c,d]
The old list after adding a new element: [a,b,c]
This is the new list: [a,b,c] The new list after adding a new element: [a,b,c,d] The old list after adding a new element: [a,b,c]

Enter fullscreen mode Exit fullscreen mode

Copy list using equal operator

We can use the = operator to copy the list. The only drawback to this method is that it does not create a shallow copy.

That said, if we make any modifications to the new list, the old list will also get modified.

Input:

#Defining a list
list = [a,b,c]
#Copying list
list_new = list
print ("This is the new list: " + str(list_new))
list_new.append(d)
print ("The new list after adding a new element: " + str(list_new))
print ("The old list after adding a new element" + str(list))
#Defining a list
list = [a,b,c]

#Copying list
list_new = list

print ("This is the new list: " + str(list_new))

list_new.append(d)

print ("The new list after adding a new element: " + str(list_new))
print ("The old list after adding a new element" + str(list))
#Defining a list list = [a,b,c] #Copying list list_new = list print ("This is the new list: " + str(list_new)) list_new.append(d) print ("The new list after adding a new element: " + str(list_new)) print ("The old list after adding a new element" + str(list))

Enter fullscreen mode Exit fullscreen mode

Here, we created a list and then copied the list. When you add an element to the new list, you can see that the old list is also modified as this method does not create a shallow copy.

Output:

This is the new list: [a,b,c]
The new list after adding a new element: [a,b,c,d]
The old list after adding a new element: [a,b,c,d]
This is the new list: [a,b,c]
The new list after adding a new element: [a,b,c,d]
The old list after adding a new element: [a,b,c,d]
This is the new list: [a,b,c] The new list after adding a new element: [a,b,c,d] The old list after adding a new element: [a,b,c,d]

Enter fullscreen mode Exit fullscreen mode

Using built-in method list() to copy list

Another way to copy a list in Python is to use the built-in method list(). It also creates a shallow copy which means any time a modification is made in the new list, it will not show on the old list.

Input:

#Defining a list
list = [a,b,c]
#Copying list
list_new = list(list)
print ("This is the new list: " + str(list_new))
list_new.append(d)
print ("The new list after adding a new element: " + str(list_new))
print ("The old list after adding a new element" + str(list)
#Defining a list
list = [a,b,c]

#Copying list
list_new = list(list)

print ("This is the new list: " + str(list_new))

list_new.append(d)

print ("The new list after adding a new element: " + str(list_new))
print ("The old list after adding a new element" + str(list)
#Defining a list list = [a,b,c] #Copying list list_new = list(list) print ("This is the new list: " + str(list_new)) list_new.append(d) print ("The new list after adding a new element: " + str(list_new)) print ("The old list after adding a new element" + str(list)

Enter fullscreen mode Exit fullscreen mode

Here, we created a list and then copied the list. When you add an element to the new list, you can see that the old list does not show the modification.

Output:

This is the new list: [a,b,c]
The new list after adding a new element: [a,b,c,d]
The old list after adding a new element: [a,b,c]
This is the new list: [a,b,c]
The new list after adding a new element: [a,b,c,d]
The old list after adding a new element: [a,b,c]
This is the new list: [a,b,c] The new list after adding a new element: [a,b,c,d] The old list after adding a new element: [a,b,c]

Enter fullscreen mode Exit fullscreen mode

Closing thoughts

Copy list is an easy way to copy in Python. It is a built-in method that makes writing code easier. You can use the other methods mentioned above but make sure you know about the shallow copy. One can read about more Python concepts here.

Python Tutorials (31 Part Series)

1 How to use ‘not equal’ operator in Python?
2 How to use exponents in Python?
27 more parts…
3 How to check if file exists in Python?
4 How to represent an infinite number in Python?
5 Various ways to sum numbers in Python
6 Python – pass by reference
7 4 Ways to implement Python Switch Case Statement
8 How to create a list of lists in Python?
9 Python regex: How to search and replace strings
10 How to copy a list in Python?
11 Python rename file: How to rename a file in Python
12 How to calculate the length of an array in Python?
13 How to use Open() in Python?
14 How to sort dictionary by value in Python?
15 How to get Timestamp in Python?
16 How to block a comment in Python?
17 How does return() in Python work?
18 Python constructors – How to use them?
19 Python append to string – How is it done?
20 Python list contains: How to check if an item exists in list?
21 Python Initialize List – How to do it?
22 How to print a list in Python?
23 Python remove file – How to delete a file?
24 What are Identifiers in Python?
25 Python Reserved Words List – Your Complete Guide
26 Different Arithmetic operators in Python
27 Different Comparison operators in Python
28 Different Assignment operators in Python
29 What are the Control Statements in Python?
30 Looping Statements in Python
31 What is a while loop in Python?

原文链接:How to copy a list in Python?

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
If you get tired, learn to rest, not to quit.
如果你累了,学会休息,而不是放弃
评论 抢沙发

请登录后发表评论

    暂无评论内容