🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #56

Remove Set Items


Remove Item

To remove an item in a set, use the remove(), or the discard() method.

Example

thisset = {"apple", "banana", "cherry"}

thisset.remove("banana")

print(thisset)

Note: If the item to remove does not exist, remove() will raise an error.

Example

thisset = {"apple", "banana", "cherry"}

thisset.discard("banana")

print(thisset)

Note: If the item to remove does not exist, discard() will NOT raise an error.

You can also use the pop() method to remove an item, but this method will remove a random item, so you cannot be sure what item that gets removed.

The return value of the pop() method is the removed item.

Example

  thisset = {"apple", "banana", "cherry"}

  x =
  thisset.pop()

print(x)

  print(thisset)

Note: Sets are unordered, so when using the pop() method, you do not know which item that gets removed.

Example

thisset = {"apple", "banana", "cherry"}

thisset.clear()

print(thisset)

Example

thisset = {"apple", "banana", "cherry"}

del
thisset

print(thisset)

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Remove Set Items – FAQs

Quick answers about learning Remove Set Items in Python.

This free note from CodingNow 2.0 explains Remove Set Items in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including Remove Set Items, is 100% free with no signup required.
With focused practice, most students grasp Remove Set Items in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now