Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Add/Remove Set Items

Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Add/Remove Set Items

Add or Remove Items from a Set

In Python, sets are flexible when it comes to adding or removing items — but remember, they’re still unordered and don’t allow duplicates. Let’s explore how to manage items in a set.

Adding Items to a Set

Add One Item Using add()

If you want to drop a single value into your set, use the .add() method.

				
					countries = {"Brazil", "Canada", "Norway"}
countries.add("Egypt")
print(countries)

				
			

Output:

				
					{'Egypt', 'Brazil', 'Canada', 'Norway'}

				
			

The new item is inserted — but since sets are unordered, its position may vary.

Add Multiple Items Using update()

To insert several values at once, use update(). It can take another set, list, tuple, or even a dictionary (keys only).

				
					countries = {"Brazil", "Canada", "Norway"}
new_countries = {"Egypt", "Greece", "Kenya"}
countries.update(new_countries)
print(countries)

				
			

Output:

				
					{'Egypt', 'Greece', 'Canada', 'Brazil', 'Kenya', 'Norway'}

				
			

Each time you run the code, the order of items printed may change — and that’s normal for sets.

Removing Items from a Set

Remove an Item Using remove()

If the item exists, it will be deleted. If not, Python throws an error.

				
					tools = {"Hammer", "Drill", "Saw", "Wrench"}
tools.remove("Drill")
print(tools)

				
			

Output:

				
					{'Wrench', 'Hammer', 'Saw'}

				
			

But what if we try removing something that’s not in the set?

				
					tools.remove("Chisel")

				
			

Output:

				
					KeyError: 'Chisel'

				
			
Safe Removal Using discard()

If you want to avoid errors, use discard(). If the item doesn’t exist, Python simply moves on.

				
					tools = {"Hammer", "Drill", "Saw", "Wrench"}
tools.discard("Chisel")
print(tools)

				
			

Output:

				
					{'Hammer', 'Saw', 'Drill', 'Wrench'}

				
			

Random Removal Using pop()

Since sets are unordered, .pop() removes and returns a random item.

				
					books = {"Math", "English", "Physics", "Biology"}
removed = books.pop()
print("Removed:", removed)
print("Remaining:", books)

				
			

Output (may vary):

				
					Removed: Physics  
Remaining: {'Math', 'English', 'Biology'}

				
			

Delete Entire Set Using del

If you want to delete the whole set, use the del keyword.

				
					brands = {"Nike", "Adidas", "Puma"}
del brands
print(brands)

				
			

Output:

				
					NameError: name 'brands' is not defined

				
			

Once deleted, the variable is gone.

Clear All Items Using clear()

This keeps the set alive but empties it.

				
					brands = {"Nike", "Adidas", "Puma"}
brands.clear()
print(brands)

				
			

Output:

				
					set()

				
			

Check if an Item Exists

You can check whether something exists in the set using the in keyword.

				
					students = {"Ali", "Hira", "Usman", "Kiran"}

if "Hira" in students:
    print("Hira is enrolled.")
else:
    print("Hira is not enrolled.")

				
			

Output:

				
					Hira is enrolled.

				
			
				
					if "Fahad" in students:
    print("Fahad is enrolled.")
else:
    print("Fahad is not enrolled.")

				
			

Output:

				
					Fahad is not enrolled.

				
			

Summary

  • Use .add() for one item, .update() for many.

  • Use .remove() to delete safely only if you’re sure the item exists, otherwise prefer .discard().

  • .pop() removes a random item.

  • Use del to delete the whole set and .clear() to empty it.

  • Use in to check membership.

Sets are efficient and fast — and now you know how to modify them without breaking your code.

 

Keep practicing — only at Learn With Arshyan.

Scroll to Top