You are currently viewing Python Set copy() Method

Python set copy() method creates a shallow copy of the set and returns the copied set. This method does not take any parameters and returns a new set object. A shallow copy means that a new set is created with the same elements as the original set, but the elements themselves are not copied, instead, the new set contains references to the same objects as the original set. So any changes to the copied set won’t reflect on the original set.

Advertisements

In this article, I will explain the Python set copy() method syntax, parameters, and usage of how to copy all the elements from a given set with examples.

1. Quick Examples of Set copy() Method

If you are in a hurry, below are some quick examples of the set copy() method.


# Quick examples of set copy() method

# Example 1: Copying a set
# Using the copy() method
myset = {"Python", "Spark", "Hadoop", "C++"}
copied_set = myset.copy()

# Example 2: Copying a set 
# Using = operator
myset = {"Python","Spark", "Hadoop","C++"}
copied_set = myset

# Example 3: Using set() constructor
copied_set = set(myset)  

2. Python Set copy() Method

set.copy() method is used to create a shallow copy of a set. It returns a new set object with the same elements as the original set. This method does not allow any arguments and returns a new set object that contains references to the same objects as the original set.

2.1 Syntax of Set copy() Method

Following is the syntax of the set copy() method.


# Syntax of set copy() function
set.copy()

2.2 Parameter of copy()

The set copy() method does not take any parameter.

2.3 Return Value

It returns a new set object that contains the same elements as the original set and does not modify the original set itself.

3. Copying a Set using the copy() Method

You can copy a set using the copy() method in Python. First, define a set called myset with four elements. Then, you use the copy() method to create a copy of the set and assign it to the variable copied_set. Finally, you print the original set and the copied set to verify the result.

Note: If you modify the copied set changes, won’t be reflected in the original set.


# Initialize set
myset = {"Python", "Spark", "Hadoop", "C++"}
print("Original set: ", myset)

# Copying a set
# Using the copy() method
copied_set = myset.copy()
print("Copied set: ", copied_set)

Yields below output.

Python set copy

4. Add Element to the Set After using copy()

The copy() method creates a shallow copy of a set in Python, which means it creates a new set object with the same elements as the original set. However, after using copy(), you can still modify the original set or the copied set independently. If you want to add items to the set after using copy(), you can do so by directly modifying either the original set or the copied set.


# Adding items to the original set
myset.add("Java")
myset.add("PHP")

# Adding items to the copied set
copied_set.add("Scala")
copied_set.add("C#")

print("Original set:", myset)
print("Copied set:", copied_set)
Python set copy

5. Other Ways to Copy Set Elements

Besides the copy() method, you can also copy the elements using = assignment operator and set() constructor. Let’s see these with examples.

5.1 Using the set() Constructor

You can use the set() constructor to copy the elements which is similar to the copy() method. For instance, the set() constructor or the copy() method creates a new set object with the same elements as the original set, allowing you to modify one set without affecting the other.


# Initialize set
myset = {"Python","Spark", "Hadoop","C++"}
print("Original set: ", myset)

# Using set() constructor
copied_set = set(myset)  

# Add an element to set
copied_set.add("Java")
print("Copied set: ", copied_set)

# Output:
# Original set:  {'C++', 'Python', 'Hadoop', 'Spark'}
# Copied set:  {'Java', 'Python', 'C++', 'Spark', 'Hadoop'}

5.2 Copying a Set Using = Operator

Unlink the above two methods the behavior of the = assignment operator is different. Use the = assignment operator to assign a set to a new variable, with this, you are not creating a copy of the set. Instead, both the original set and the new variable will refer to the same set object. Any changes made to the set through one variable will be reflected in the other variable as well.


# Initialize set
myset = {"Python","Spark", "Hadoop","C++"}

# Copy set using =
copied_set = myset

# Copying a set 
# Using = operator
copied_set.add("Java")
print("Original set: ", myset)
print("Copied set: ", copied_set)

Yields below output.

Python set copy

6. Conclusion

In this article, I have explained the Python set copy() method and using its syntax, parameters learned how to copy all the elements from a given set with examples. This method does not take any parameters and returns a new set object.

The copy() performs a shallow copy meaning a new set is created with the same elements as the original set, but the elements themselves are not copied. Instead, the new set contains references to the same objects as the original set. So any changes to the copied set won’t reflect on the original set.

Happy Learning !!