In the previous article, we have discussed Python List clear() Method with Examples
List in Python:
Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.
List copy() Method in Python:
The copy() method returns a duplicate (copy) of the specified list.
Syntax:
list.copy()
Parameter Values: This method has no parameters.
Return Value:
A new list is returned by the copy() function. It makes no changes to the original list.
Examples:
Example1:
Input:
Given List = ['hello', 'this', 'is', 'btechgeeks']
Output:
The given list is : ['hello', 'this', 'is', 'btechgeeks'] The new list after applying copy() function is : ['hello', 'this', 'is', 'btechgeeks']
Example2:
Input:
Given List = [20, 40, 60, 80, 100]
Output:
The given list is : [20, 40, 60, 80, 100] The new list after applying copy() function is : [20, 40, 60, 80, 100]
List copy() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the list as static input and store it in a variable.
- Print the given list.
- Copy all the elements of the given list into the new list using the copy() function and store it in another variable.
- Print the above new list which is the duplicate of the given list.
- The Exit of Program.
Below is the implementation:
# Give the list as static input and store it in a variable. gvn_lst = ["hello", "this", "is", "btechgeeks"] # Print the given list. print("The given list is : ", gvn_lst) # Copy all the elements of the given list into the new list using the copy() # function and store it in another variable. new_lst = gvn_lst.copy() # Print the above new list which is the duplicate of the given list. print("The new list after applying copy() function is :") print(new_lst)
Output:
The given list is : ['hello', 'this', 'is', 'btechgeeks'] The new list after applying copy() function is : ['hello', 'this', 'is', 'btechgeeks']
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Print the given list.
- Copy all the elements of the given list into the new list using the copy() function and store it in another variable.
- Print the above new list which is the duplicate of the given list.
- The Exit of Program.
Below is the implementation:
# Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Print the given list. print("The given list is : ", gvn_lst) # Copy all the elements of the given list into the new list using the copy() # function and store it in another variable. new_lst = gvn_lst.copy() # Print the above new list which is the duplicate of the given list. print("The new list after applying copy() function is :") print(new_lst)
Output:
Enter some random List Elements separated by spaces = 20 40 60 80 100 The given list is : [20, 40, 60, 80, 100] The new list after applying copy() function is : [20, 40, 60, 80, 100]
copying List using ‘=’ Operator
To copy a list, we can also use the = operator.
However, there is one drawback to copying lists in this manner. When you change a new list, you also change the old list. It’s because the new list refers to or points to the same old list object.
Example:
Approach:
- Give the list as static input and store it in a variable.
- Copy the elements of the given list into the new list using the ‘=’ operator.
- Store it in another variable.
- Add an element to the new list using the append() function.
- Print the given original list.
- Print the new list obtained.
- The Exit of Program.
Below is the implementation:
# Give the list as static input and store it in a variable. gvn_lst = [20, 40, 60, 80, 100] # copy the elements of the given list into new list using the '=' operator # store it in another variable. new_lst = gvn_lst # Add an element to the new list using the append() function new_lst.append('python') # Print the given original list. print("The given original list is:", gvn_lst) # Print the new list obtained. print("The new list obtained is :", new_lst)
Output:
The given original list is: [20, 40, 60, 80, 100, 'python'] The new list obtained is : [20, 40, 60, 80, 100, 'python']
copying List using Slicing
For Example:
# Give the list as static input and store it in a variable. gvn_lst = ['good', 'morning', 'btechgeeks', 123] # copy the elements of the given list into new list using the slicing. # store it in another variable. new_lst = gvn_lst[:] # Add an element to the new list using the append() function new_lst.append(1000) # Print the given original list. print("The given original list is:", gvn_lst) # Print the new list obtained. print("The new list obtained is :", new_lst)
Output:
The given original list is: ['good', 'morning', 'btechgeeks', 123] The new list obtained is : ['good', 'morning', 'btechgeeks', 123, 1000]
Know about the syntax and usage of functions to manipulate lists with the provided Python List Method Examples and use them.