In this Python tutorial, we’ll learn about Python set copy method in detail. We’ll be using proper examples in order to better understand the role of copy method in Python set.
Introduction: Python Set Copy Method
As the name suggests, this method is used to make a copy of the Python set. We’ll first understand it syntax in detail so it’ll be easy for us to understand its implementation in the below examples.
Let’s now understand the use of Python set copy method using practical Python examples.
Syntax of Copy Method
set.copy()
- Copy method does not take any arguments.
- This method return a copy of the specified Python set.
Implementing Python Set Copy Method (Easy Examples)
Let’s now understand the practical use of copy() method in Python set.
Example 1: Copy a Python Set
sSet = {9,7,6,4,6,'Umair',39.7,23.9,('a','b',4,7),45,20} print( sSet.copy() )
Output
{4, 6, 7, 39.7, 9, 'Umair', ('a', 'b', 4, 7), 45, 20, 23.9}
We’ve created a Python set and applied our copy() method on it. We’ve used it inside Python print function in order to display its value. As a result, a copy of that specific set is displayed.
We can also store the copied set in a variable. See below code:
copiedSet = sSet.copy() print( sSet )
Output
{4, 6, 7, 39.7, 9, 'Umair', ('a', 'b', 4, 7), 45, 20, 23.9}
Example 2: Copy Set Using Python Assignment Operator (=)
We can also easily copy an existing Python set using the assignment operator. See below code:
a={2,5,6} b=a print('Original: ',a) print('Copied: ',b)
Output
Original: {2, 5, 6} Copied: {2, 5, 6}
We’ve created a simple Python set. After that, we’ve created a variable and have assigned the set to it using assignment operator. In order to prove it, we’ve displayed values of both these variables using Python print function.
Example 3: Return Value of Python Set Copy Method
The copy() method returns the copy of the specified Python set. See below example for better understanding.
s = {8,7,9,'a','h','d',('a',4,6),4.6} returnVal = s.copy() print(returnVal)
Output
{'a', 4.6, 7, 8, 9, 'd', ('a', 4, 6), 'h'}
- We’ve created a Python set.
- After that, we’ve used its copy() method and have assigned the returned value to a variable. It means whatever the copy method returns, it’ll be stored in that variable.
- Finally, we’ve printed value of that variable using print function. As a result, it displayed the whole copy of existing Python set which means that Python set copy method returns the whole copy of the specified Python set.
Example 4: Copy a List of Sets Using For Loop
sList=[{2,3,4},{7,8},{'a','b'},{9,'abc',4.6},{98.8,2.1}] copiedSetList=[] for var in sList: copiedSetList.append(var.copy()) print(copiedSetList)
Output
[{2, 3, 4}, {8, 7}, {'b', 'a'}, {9, 4.6, 'abc'}, {98.8, 2.1}]
- We’ve created a list of Python sets.
- After that, we’ve created an empty Python list. This list will be used to store the list of copied sets.
- Then we’ve used Python for loop which will take every item of list(in our case that item is a set), apply Python set copy method on it and append it to the new list.
- Finally, we’ve printed the new Python list in order to prove that all the items are copied successfully.
So this is how we can properly use Python set copy method. If you enjoyed this post and have learned anything new from reading it, then do share it with your team of Python programmers.
Share your experience with us regarding this method.
We’ll also be happy to help you if you have questions regarding the implementation of Python set copy method.
Conclusion
To conclude our Python tutorial, we’ve uncovered the role and proper usage of Python set copy method. We’d be very happy to receive your valuable thoughts on this post.
You can discover additional details about Python programming by visiting our other posts. Thank you for reading this article.