Python Add Set Items | How to Add Items to a Set with Examples

Python Add Set Items

Adding items to a Python set means inserting new elements into the set. Since sets are unordered and do not allow duplicates, adding an existing element does not change the set. There are mainly two methods to add items:

Python set में items जोड़ने का मतलब नए elements insert करना होता है। चूंकि set unordered होता है और duplicates allow नहीं करता, इसलिए existing item जोड़ने पर set नहीं बदलेगा। मुख्य रूप से दो तरीके हैं items जोड़ने के:

Methods to Add Items to a Set

  • add(item): Adds a single item to the set.
  • update(iterable): Adds multiple items from an iterable (list, tuple, set, etc.).
  • add(item): सेट में एक item जोड़ता है।
  • update(iterable): किसी iterable (list, tuple, set आदि) से कई items एक साथ जोड़ता है।
Example 1: Add Single Item Using add()
myset = {1, 2, 3}
myset.add(4)
print(myset)

Add 4 to the set using add().

add() से set में 4 जोड़ें।

Output:आउटपुट:

{1, 2, 3, 4}
Example 2: Add Duplicate Item Using add() (No Change)
myset = {1, 2, 3}
myset.add(2)
print(myset)

Adding duplicate item 2 does not change the set.

Duplicate item 2 जोड़ने पर set नहीं बदलेगा।

Output:आउटपुट:

{1, 2, 3}
Example 3: Add Multiple Items Using update() with List
myset = {1, 2}
myset.update([3, 4, 5])
print(myset)

Add multiple items from a list using update().

update() से list के कई items जोड़ें।

Output:आउटपुट:

{1, 2, 3, 4, 5}
Example 4: Add Multiple Items Using update() with Tuple
myset = {10}
myset.update((20, 30))
print(myset)

Add multiple items from a tuple using update().

update() से tuple के कई items जोड़ें।

Output:आउटपुट:

{10, 20, 30}
Example 5: Add Items Using update() with Set
myset = {100}
myset.update({200, 300})
print(myset)

Add multiple items from another set using update().

update() से दूसरे set के items जोड़ें।

Output:आउटपुट:

{100, 200, 300}
Example 6: Add Characters Using update() with String
myset = {'a', 'b'}
myset.update('cd')
print(myset)

Add each character of a string to the set using update().

update() से string के प्रत्येक character जोड़ें।

Output:आउटपुट:

{'a', 'b', 'c', 'd'}
Example 7: Add Items from Range Using update()
myset = {1}
myset.update(range(2, 5))
print(myset)

Add multiple items from a range using update().

update() से range के कई items जोड़ें।

Output:आउटपुट:

{1, 2, 3, 4}
Example 8: Add Items One by One Using add() in Loop
myset = set()
for i in [5, 6, 7]:
    myset.add(i)
print(myset)

Add items one by one in a loop using add().

loop में add() से एक-एक item जोड़ें।

Output:आउटपुट:

{5, 6, 7}
Example 9: Adding Immutable Tuple to Set
myset = {(1, 2)}
myset.add((3, 4))
print(myset)

Add immutable tuple as an item to the set.

set में immutable tuple जोड़ें।

Output:आउटपुट:

{(1, 2), (3, 4)}
Example 10: Trying to Add Mutable List Causes Error
myset = {1, 2}
# myset.add([3, 4])  # This will cause TypeError
print(myset)

You cannot add a mutable list directly to a set because lists are unhashable.

mutable list को set में सीधे नहीं जोड़ सकते क्योंकि list unhashable होती है।

Output:आउटपुट:

{1, 2}
# TypeError: unhashable type: 'list'