B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python Copy Sets with Examples | LiveCodeProgramming
LiveCodeProgramming

Python Copy Sets

In Python, you can copy a set using the copy() method or the set() constructor. This creates a new set with the same elements.

Python में आप copy() method या set() constructor का उपयोग करके एक set की copy बना सकते हैं। इससे एक नया set बनता है जिसमें वही elements होते हैं।

1. Using copy() Method

Creates an exact duplicate of a set.

Set की exact copy बनाता है।

original = {"apple", "banana", "cherry"}
copy_set = original.copy()
print(copy_set)
Output:
{'banana', 'cherry', 'apple'}
2. Using set() Constructor

Also creates a new set with same items.

Set constructor भी एक नई copy बनाता है।

original = {"x", "y", "z"}
new_set = set(original)
print(new_set)
Output:
{'x', 'y', 'z'}
Note: Both copy() and set() return shallow copies. If you modify the new set, it does not affect the original.