You can remove elements from a Python set using several methods like remove()
, discard()
, pop()
, or clear()
.
Python set से elements हटाने के लिए remove()
, discard()
, pop()
, या clear()
method का उपयोग किया जाता है।
remove()
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits)
Output:
{'apple', 'cherry'}
discard()
fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
print(fruits)
Output:
{'apple', 'cherry'}
pop()
fruits = {"apple", "banana", "cherry"}
item = fruits.pop()
print("Removed:", item)
print(fruits)
Output:
Removed: cherry {'apple', 'banana'}
clear()
fruits = {"apple", "banana", "cherry"}
fruits.clear()
print(fruits)
Output:
set()