B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Remove Set Items in Python | LiveCodeProgramming
LiveCodeProgramming

Remove Items from Set in Python

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 का उपयोग किया जाता है।

1. Using remove()
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits)
Output:
{'apple', 'cherry'}
2. Using discard()
fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
print(fruits)
Output:
{'apple', 'cherry'}
3. Using pop()
fruits = {"apple", "banana", "cherry"}
item = fruits.pop()
print("Removed:", item)
print(fruits)
Output:
Removed: cherry
{'apple', 'banana'}
4. Using clear()
fruits = {"apple", "banana", "cherry"}
fruits.clear()
print(fruits)
Output:
set()