Skip to content

Python Set | What are Sets in Python ? (Best 5 Minute Revision)

New to Python? Let’s master Set together! Learn how to create, use, and apply set operations for clean, efficient coding. Understand Python Set with examples, tips, and real use cases. Start writing cleaner code today!

What is Python Set ?

A Python Set is used to store multiple items in a single variable. It is an unordered collection of unique items. It is one of the 4 collection literals in Python, the other 3 are Tuple, List and Dictionary

A set is an unordered, unique and unchangeable collection type. They are useful for performing mathematical set operations, such as union, intersection, symmetric difference, etc. Sets in Python has a lot of operational methods for the same, discussed later in this article.

Key Features of Python Set

  • Unordered: The items in a set have no defined order and can be arranged in any manner.

  • Unique Items: No duplicate values are allowed in a set. Even if you add any duplicate value then it overwrites the existing one.

  • Immutable: You can add or remove items after creation but you cannot update or overwrite them.

  • Heterogeneous: You can store different types of data types in a single set.

Sets are defined using curly braces {} or the built-in set() function. For example, 

      numbers = {1, 2, 3}
      fruits = set([“apple”, “banana”, “cherry”])

You can’t access set items by index because sets are unordered. But you can loop through them:

      for fruit in fruits:
                print(fruit)

sets

Set Methods

Sets in Python has following methods to perform operations:  

1.add(x) – Adds an item to the set. For example,

 fruits.add(“orange”)

2.update(iterable) – Adds multiple items at a same time in a set. For example, 

 fruits.update([“grape”, “melon”])

3.remove(x)/ discard(x) – Removes item from the set and if not found, raises error. For example,

 fruits.remove(“cherry”) # Raises error if not found
 fruits.discard(“banana”) # No error if not found

4.clear() – Empties the whole set. For example,

 fruits.clear() # Removes all items

5.union(),  intersection(), difference(), symmetric_difference() – These are all set operations that we can perform for various mathematical and data analytical needs. For example,

 set1 = {1, 2, 3}
 set2 = {3, 4, 5}

 print(set1 | set2) # Output: {1, 2, 3, 4, 5} – UNION

 print(set1 & set2) # Output: {3} – INTERSECTION

 print(set1 – set2) # Output: {1, 2} – DIFFERENCE

 print(set1 ^ set2) # Output: {1, 2, 4, 5} – SYMMETRIC DIFFERENCE

6.issubset(), issuperset(), isdisjoint() – Comparison methods in set. For example,

 a = {1, 2}
b = {1, 2, 3, 4}

#Checks if all elements of one set are present in another.

print(a.issubset(b)) # Output: True

#Checks if a set contains all elements of another set.

print(b.issuperset(a)) # Output: True

#Returns True if sets have no elements in common.

print(a.isdisjoint(b)) # Output: False

.

Use Cases of Python Sets

  • Removing duplicates from a list

  • Performing set operations like union, intersection

  • Efficient membership testing

  • Storing unique values (like user IDs, tags, etc.)

 

FAQs

Q1. Can a Python set have duplicate values?
No, all elements in a set are unique.

Q2. Can I access set elements by index?
No, sets are unordered and don’t support indexing.

Q3. Can a Python set contain different data types?
Yes, as long as they are hashable (immutable types like numbers, strings, tuples).

Q4. How do I remove duplicates from a list using a set?

unique_list = list(set(my_list))

Q5. Is a Python set mutable?
Yes, you can add and remove elements from a set.

Conclusion

The Python Set is a powerful data structure ideal for managing unique, unordered data. Whether you’re filtering duplicates or performing quick membership tests, sets offer an efficient and intuitive solution.

Understanding how to use sets will level up your coding skills and simplify many programming tasks. Try experimenting with them in your next Python project!

If you’re serious about learning Python, make sure to visit and explore our Python for beginners article — it includes exercises, examples, and a complete roadmap to mastering Python, step by step.

Keep growing, Keep learning !!

Scroll to Top