The copy() method in Python Set is used to create a shallow copy of the set. It returns a new set with the same elements as the original set. The new set is a completely independent object with its own memory.
Basic Syntax
new_set = set.copy()
Here, set is the original set from which a copy has to be created, and new_set is the new set that will be created as a copy of the original set.
Return Value
The copy() method returns a new set with the same elements as the original set.
Examples
Example 1: Copy a set
In this example, we will create a copy of a set using the copy() method.
# Creating a set fruits = {"apple", "banana", "cherry"} # Creating a copy of the set new_fruits = fruits.copy() # Printing both sets print("Original Set: ", fruits) print("Copied Set: ", new_fruits)
The output of the above code will be:
Original Set: {'cherry', 'banana', 'apple'} Copied Set: {'cherry', 'banana', 'apple'}
As we can see, the copy() method has created a new set new_fruits that is a copy of the original set fruits.
Example 2: Modify the original set after creating a copy
In this example, we will create a copy of a set and then modify the original set. We will see that the copy remains unaffected by the changes made to the original set.
# Creating a set fruits = {"apple", "banana", "cherry"} # Creating a copy of the set new_fruits = fruits.copy() # Modifying the original set fruits.add("orange") # Printing both sets print("Original Set: ", fruits) print("Copied Set: ", new_fruits)
The output of the above code will be:
Original Set: {'cherry', 'banana', 'orange', 'apple'} Copied Set: {'cherry', 'banana', 'apple'}
As we can see, the original set fruits has been modified by adding a new element “orange”, but the copied set new_fruits remains unaffected.
Example 3: Nested sets
In this example, we will create a nested set and create a copy of it. We will modify the original set and see that the copied set remains unaffected.
# Creating a nested set fruits = {"apple", "banana", {"cherry", "orange"}} # Creating a copy of the set new_fruits = fruits.copy() # Modifying the original set fruits.discard("banana") # Printing both sets print("Original Set: ", fruits) print("Copied Set: ", new_fruits)
The output of the above code will be:
Original Set: {'apple', {'cherry', 'orange'}} Copied Set: {'apple', {'cherry', 'orange'}}
As we can see, the original set fruits has been modified by removing the element “banana”, but the copied set new_fruits remains unaffected.
Use Cases
- The `copy()` method is used to create a new set that is a copy of the original set without modifying the original set.
- The copy() method is useful when we want to perform operations on the set without affecting the original set.
- The copy() method can be used to create a backup of a set before performing any operations on it.
In conclusion, the copy() method in Python Set is a useful method when we want to create a copy of a set without modifying the original set. It returns a new set with the same elements as the original set, which is a completely independent object with its own memory. The method is useful when we want to perform operations on the set without affecting the original set or to create a backup of a set before performing any operations on it.