RS Punia🐍
RS Punia🐍

@CodingMantras

18 Tweets Feb 19, 2023
Python Set: (Day 4)
`copy()` method in sets.
The copy() method is a built-in set method that returns a shallow copy of the set.
Syntax:
s.copy()
How does the copy() method work? What are some other methods to copy a set?
Let's check outπŸ‘‡
Working steps of the copy() method:
Step 1: Creates a new empty set
The copy() method creates a new empty set that will be used to store the copied elements.
Step 2: Iterates over the original set
The copy() method then iterates over each element in the original set and adds it to the new set (Add the reference to that element).
Step 3: Return the new set
Once all elements have been copied, the copy() method returns the new set.
For example:
# create a set of numbers
original_set = {1, 2, 3, 4, 5}
# create a copy of the set using the copy() method
copied_set = original_set.copy()
# Add a new number to the original set
original_set.add(6)
# Print both sets to see the difference
print(original_set) # Output: {1, 2, 3, 4, 5, 6}
print(copied_set) # Output: {1, 2, 3, 4, 5}
Here, we create a set of numbers `original_set` and then create a copy of the set `copied_set` using the copy() method.
We then add a new number to the original set and print both sets to see the difference.
We can see the original set has the additional element while the copied set remains unchanged.
Note: The copy() method creates a shallow copy of the set which means that the elements themselves are not copied.
Instead both the original set and the copied set will reference the same objects.
An example to demonstrate that the ID of the original set and the copied set are different but the ids of the elements are the same:
# print the IDs of the original set and the copied set
print("Original set id:", id(original_set))
print("Copied set id:", id(copied_set))
Output:
# Original set id: 140113763621792
# Copied set id: 140113764694944
As you can see from the output, the IDs of the original set and the copied set are different which confirms that they are distinct objects in memory.
But:
# print the IDs of the original & copied elements
print("Original set element ids:", [id(x) for x in original_set])
print("Copied set element ids:", [id(x) for x in copied_set])
Output:
# Original set element ids: [9793088, 9793120, 9793152, 9793184, 9793216]
# Copied set element ids: [9793088, 9793120, 9793152, 9793184, 9793216]
We can see that the ids of the `original_set` and the `copied_set` elements are the same.
Which means both sets reference the same elements in the memory.
Other ways to achieve the same functionality as the `copy()` method in sets:
1. Set constructor:
You can use the set constructor to create a new set that is a copy of an existing set. For example:
original_set = {1, 2, 3}
new_set = set(original_set)
2. Set union
You can use the set union operator (|) to combine two sets into a new set. For example:
original_set = {1, 2, 3}
new_set = original_set | set()
3. Unpacking operator (*)
The unpacking operator (*) is used to pass the elements of the original_set to `{}`, which creates a new set with the same elements.
original_set = {1, 2, 3}
new_set = {*original_set}
All of these approaches will create a new set that is a copy of the original set, without modifying the original set.
That's all for this thread.
I tweet daily about Python Fundamentals & Machine Learning πŸπŸš€
If you enjoy reading this:
βœ… Retweet the first tweet so others find it too
βœ… Follow @CodingMantras
for more on Python
It encourages me to keep coding and sharing!

Loading suggestions...