In Python, you can join two or more sets using methods like union()
, update()
, set unpacking using *
, and using for
loop.
Python में आप दो या अधिक sets को union()
, update()
, *
unpacking, या for
loop से जोड़ सकते हैं।
union()
Returns a new set combining all elements.
यह नया set बनाता है जिसमें दोनों sets के elements होते हैं।
a = {"apple", "banana"}
b = {"cherry", "banana"}
result = a.union(b)
print(result)
Output:
{'apple', 'banana', 'cherry'}
update()
Adds elements of another set to current one.
दूसरे set के elements को current set में जोड़ता है।
a = {"A", "B"}
b = {"C", "D"}
a.update(b)
print(a)
Output:
{'A', 'B', 'C', 'D'}
*
UnpackingYou can merge multiple sets into a new one.
आप कई sets को एक साथ unpack करके नया set बना सकते हैं।
s1 = {"x", "y"}
s2 = {"z"}
merged = {*s1, *s2}
print(merged)
Output:
{'x', 'y', 'z'}
Add items manually from another set.
दूसरे set से items को loop द्वारा जोड़ सकते हैं।
set1 = {"red"}
set2 = {"blue", "green"}
for item in set2:
set1.add(item)
print(set1)
Output:
{'red', 'blue', 'green'}