Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Example of Python Sets
3.
Type Casting with Python Set Method
4.
Check Unique & Immutable with Python Set
4.1.
Understanding Uniqueness in Sets
4.2.
Exploring Immutability in Set Items
4.3.
Heterogeneous Elements with Python Set
5.
Benefits & Limitations
6.
Internal Working of Set
6.1.
Python
7.
Methods for Sets
7.1.
add(element)
7.2.
Python
7.3.
remove(element)
7.4.
discard(element)
7.5.
clear()
7.6.
union(set2) & update(set2)
7.7.
intersection(set2)
8.
Adding Elements to Sets
8.1.
Using add() Method
8.2.
Python
8.3.
Using update() Method
8.4.
Python
9.
Practical Example: Removing Duplicates
9.1.
Python
9.2.
Removing Elements from Sets
9.2.1.
Using remove() Method
9.2.2.
Using discard() Method
9.3.
Python
9.3.1.
Using pop() Method
10.
Practical Example: Cleaning a Dataset
10.1.
Python
11.
Union Operation on Python Sets
11.1.
Using union() Method
11.2.
Python
11.3.
Using | Operator
12.
Practical Example: Merging Two Datasets
12.1.
Python
13.
Set Difference in Python
13.1.
Using difference() Method
13.2.
Python
13.3.
Using - Operator
14.
Practical Example: Filtering a Dataset
14.1.
Python
15.
Clearing Python Sets
15.1.
Using the clear() Method
15.2.
Python
15.3.
Why Clear a Set?
16.
Example: Using a Set in a Loop
17.
Frequently Asked Questions
17.1.
Can I store lists or dictionaries inside a Python set?
17.2.
How do I remove duplicates from a list in Python?
17.3.
Is the order of elements preserved in a Python set?
18.
Conclusion
Last Updated: Mar 27, 2024
Easy

Python Set

Author Ravi Khorwal
0 upvote

Introduction

Python sets are like bags where you can store different things, but unlike a regular bag, you can't have two of the same thing. Imagine you have a bag where every time you try to put in a second apple, it just disappears because you already have one. That's how Python sets work. They're super useful when you want to keep track of unique items or when you're doing things like comparing lists to see what's different or the same. 

Python Set

In this article, we'll explore the cool stuff you can do with Python sets, like adding stuff, combining them in different ways, and even making unchangeable sets. We'll dive into how they work behind the scenes and look at some handy tricks to use them effectively. 

Example of Python Sets

Let's kick things off with a simple example to see Python sets in action. Imagine you're collecting stickers, and you want a collection where each sticker is unique—no duplicates allowed. A Python set is perfect for this. It's like a sticker album where, no matter how many times you try to stick the same one in, you'll only have one of each type.

Here's how you can create a set in Python:

# Creating a set of stickers
stickers = {"unicorn", "dragon", "wizard", "elf"}
print(stickers)


In this example, stickers is a set with four unique elements. If you try to add another 'unicorn' sticker to it, the set will remain unchanged because 'unicorn' is already there. Let's try adding some stickers to our collection:

# Adding a new sticker
stickers.add("troll")
print(stickers)
# Trying to add a duplicate sticker
stickers.add("unicorn")
print(stickers)


After adding the 'troll' sticker, you'll see it in the set. But when you try to add another 'unicorn', the set stays the same, showcasing the uniqueness property of sets.

This simple example demonstrates the core feature of Python sets—maintaining a collection of unique items. It's straightforward but powerful, especially when you start exploring more complex operations like unions and intersections, which we'll get into later.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Type Casting with Python Set Method

Sometimes, you might find yourself with a bunch of items in a list or a tuple, and you realize, "Hey, I only need to know what's unique here." This is where Python's set method, also known as type casting, comes into play. It's like taking a box full of mixed-up Lego pieces and filtering out all the duplicates so you're left with one of each kind.

Type casting to a set is super simple. Let's say you have a list of your favorite fruits, but you're not sure if you've listed some of them more than once. Here's how you can use a set to find out which unique fruits are on your list:

# Your list of favorite fruits, possibly with duplicates
favorite_fruits = ["apple", "banana", "cherry", "apple", "banana"]
print("Original list:", favorite_fruits)
# Converting the list to a set to remove duplicates
unique_fruits = set(favorite_fruits)
print("Unique fruits:", unique_fruits)


In this snippet, favorite_fruits is a list that might have some repeats. By converting it to a set called unique_fruits, Python automatically removes any duplicates, leaving you with a set of just the unique fruit names.

This method isn't limited to just lists; it works with tuples and other iterable objects too. It's a handy trick to keep up your sleeve for when you need to clean up your data and get rid of any repeats, making your information easier to work with and understand.

Check Unique & Immutable with Python Set

Understanding Uniqueness in Sets

Imagine you're creating a list of friends to invite to a movie night. You jot down names, but to avoid sending multiple invites to the same friend, you want each name to appear only once. This is where a Python set shines. If you accidentally try to add a friend's name twice, the set automatically takes care of it by ensuring each name appears just once. Let's see this in action with some code:

# Creating a set of friends for a movie night
movie_night_friends = {"Alice", "Bob", "Charlie", "Dana"}
print("Initial set of friends:", movie_night_friends)
# Attempting to add "Alice" again
movie_night_friends.add("Alice")
print("After trying to add Alice again:", movie_night_friends)


In this example, even if we try to add "Alice" a second time, the set remains unchanged because "Alice" is already in there. This ensures the uniqueness of items in the set.

Exploring Immutability in Set Items

The term "immutable" might sound a bit fancy, but it simply means that once an item is added to a set, you can't change it. However, this doesn't mean you're stuck with your set forever as it is. You can still add new unique items or remove existing ones; you just can't alter an item that's already in there. This is like saying once you've added a friend to your invite list, you can't change their name without removing and re-adding them under a new name. Here's how you can manipulate a set:

# Adding a new friend
movie_night_friends.add("Eli")
print("After adding Eli:", movie_night_friends)
# Removing a friend
movie_night_friends.remove("Dana")
print("After removing Dana:", movie_night_friends)


By adding "Eli" and removing "Dana," we've changed the composition of our set without altering the 'unchangeable' nature of the individual items within it.

Heterogeneous Elements with Python Set

Let's say you're organizing a small treasure box. In it, you decide to put a coin (an integer), a small note with a message (a string), and a tiny, folded map (a tuple representing coordinates). Here's how you can represent this in a Python set:

# Creating a treasure box set with mixed element types
treasure_box = {25, "Secret Note", (10, 20)}
print("Contents of the treasure box:", treasure_box)


In this code snippet, 25 is an integer, "Secret Note" is a string, and (10, 20) is a tuple. Despite their differences, all these elements coexist peacefully in the treasure_box set.

Benefits & Limitations

The ability to mix elements makes sets incredibly versatile. You can track different kinds of information as a single collection without worrying about element types. However, it's essential to remember that while sets are flexible, they do have rules. For instance, all elements must still be immutable. This means you can include a tuple in a set, but not a list, as lists are mutable (changeable).

Here's an example that demonstrates adding various types of elements, including a tuple, while showing that adding a list (a mutable type) isn't allowed:

# Adding a tuple to the set
treasure_box.add((30, 40))
print("After adding a tuple of coordinates:", treasure_box)
# Trying to add a list (mutable type) to the set
try:
    treasure_box.add([1, 2, 3])  # This will raise an error
except TypeError as e:
    print("Error:", e)


In this example, adding a tuple works just fine, but attempting to add a list results in a TypeError, showcasing the set's requirement for immutable elements.

Internal Working of Set

When you add an item to a set, Python does some quick math (hashing) to decide where in its memory this item should live. This is like assigning a specific locker to each item based on its value, which makes finding and checking items super fast. Because of this hashing, sets can't have mutable (changeable) items like lists. Imagine trying to assign a locker to a chameleon that keeps changing its colors and shape!

This hashing magic is also why sets are so speedy when it comes to checking if they have a certain item. Instead of looking through each item one by one (like you would in a list), Python goes straight to the item's "locker" to see if it's there. This is a big part of why sets are great when you need to keep track of unique items and do a lot of "is this item here?" checks without slowing down your program.

Now, let's dive into some code to see this in action. Suppose we're starting with an empty set and want to add some items:

  • Python

Python

# Creating an empty set
my_set = set()

# Adding some elements
my_set.add('apple')
my_set.add('banana')
my_set.add('cherry')

# Trying to add 'apple' again
my_set.add('apple')

print(my_set)

Output

Output

Even though we tried to add 'apple' twice, our set smartly keeps just one 'apple'. This is the set's way of saying, "No duplicates allowed here!" And all this is possible because of the internal hashing mechanism that efficiently manages items.

Methods for Sets

Python sets come with a toolkit of methods that let you manage and manipulate them in all sorts of useful ways. These methods are like different commands you can give to your set to add items, remove them, or even do cool tricks like finding the items that are unique to two sets. Let's break down some of these methods and see how they work with clear examples.

add(element)

This is how you tell your set, "Hey, please take this new item." If the item is already there, the set just shrugs it off because it's all about being unique.

  • Python

Python


fruits = {'apple', 'banana'}
fruits.add('cherry') # Adding 'cherry' to the set
print(fruits) # {'apple', 'banana', 'cherry'}

Output

Output

remove(element)

Use this when you want to remove an item from your set. But be careful; if the item isn't there, Python will raise an error, kind of like telling you, "I can't remove what's not there!"

fruits.remove('banana')  # Removing 'banana'
# If 'banana' isn't in the set, this will throw an error
print(fruits)  # {'apple', 'cherry'}

discard(element)

This is the chill cousin of remove. It also removes an item, but if the item isn't there, Python won't complain; it'll just move on.

fruits.discard('banana')  # No error if 'banana' isn't found
print(fruits)  # {'apple', 'cherry'}

clear()

 When you want to wipe your set clean and start fresh, clear is your go-to method. It empties the set completely.

fruits.clear()  # The set is now empty
print(fruits)  # set()

union(set2) & update(set2)

These methods are all about bringing sets together. union gives you a new set with items from both sets, while update adds the items from one set directly into another.

set1 = {'a', 'b'}
set2 = {'b', 'c'}
set3 = set1.union(set2)  # set3 is {'a', 'b', 'c'}
set1.update(set2)  # set1 is now {'a', 'b', 'c'}

intersection(set2)

This method helps you find what's common between two sets, kind of like finding mutual friends.

set1 = {'apple', 'banana', 'cherry'}

set2 = {'google', 'microsoft', 'apple'}

common = set1.intersection(set2)  # common is {'apple'}

Adding Elements to Sets

Adding elements to a set in Python is a straightforward but powerful operation. Sets are unique because they only allow unique elements, meaning no duplicates. When you add an element to a set, Python makes sure it's not already there. This feature is incredibly useful for tasks like removing duplicates from a list or ensuring a collection contains only unique items. Let's explore the ways to add elements and some practical examples to illustrate these concepts.

Using add() Method

The add() method is your go-to for inserting a single item into a set. It's like telling your set, "Please take this one new friend."

  • Python

Python

# Initialize a set
colors = {'red', 'blue'}

# Add a new element
colors.add('green')
print(colors)

Output

Output

In this example, 'green' is added to the colors set. If 'green' was already a part of the set, Python would simply ignore the addition, keeping the set unchanged.

Using update() Method

The update() method is more sociable. It can add multiple elements at once, even from other sets, lists, or any iterable. It's like throwing a party and inviting multiple friends over, including their friends.

  • Python

Python

# Initialize a set

numbers = {1, 2, 3}

# Add multiple elements

numbers.update([4, 5], {6, 7})

print(numbers) 

Output

Output

Here, update() adds elements from a list [4, 5] and a set {6, 7} to the numbers set. The result is a set that contains all the unique elements from the original set and the iterables.

Practical Example: Removing Duplicates

One practical application of adding elements to a set is to remove duplicates from a list. Since sets automatically discard duplicates, converting a list to a set and back to a list is a common pattern for deduplication.

  • Python

Python

# Original list with duplicates

original_list = [1, 2, 2, 3, 4, 4, 5]

# Remove duplicates by converting to a set and back to a list

deduplicated_list = list(set(original_list))

print(deduplicated_list)

Output

Output

In this example, converting original_list to a set removes the duplicates, and converting it back to a list gives us a list with unique elements only.

Removing Elements from Sets

Removing elements from a set in Python is as intuitive as adding them, but with a twist. Sets are designed to be fluid and adaptable, allowing for the easy exclusion of unwanted elements. This capability is vital for tasks like filtering out specific data points or cleaning up a collection. We'll delve into the primary methods for removing elements and illustrate these with examples to ensure clarity.

Using remove() Method

The remove() method is straightforward—it deletes a specified element from the set. However, it's a bit strict; if the element doesn't exist, it raises a KeyError. It's like telling a member of the set, "You need to leave," but if they're not there, it causes a scene.

# Initialize a set
fruits = {'apple', 'banana', 'cherry'}
# Remove an element
fruits.remove('banana')
print(fruits)  # Output: {'apple', 'cherry'}
# Trying to remove a non-existent element
# fruits.remove('mango')  # This would raise a KeyError


In this scenario, 'banana' is successfully removed from the fruits set. Attempting to remove 'mango', which is not in the set, would result in an error.

Using discard() Method

The discard() method is more forgiving. It also removes a specified element, but if the element isn't present, it simply does nothing. It's akin to saying, "If you're here, please leave; if not, no worries."

  • Python

Python

# Initialize a set
gadgets = {'phone', 'tablet', 'laptop'}

# Remove an element safely
gadgets.discard('tablet')
print(gadgets)

Output

Output

# Attempt to discard a non-existent element

gadgets.discard('camera')  # No error is raised

Here, 'tablet' is removed from the gadgets set without any issue, and attempting to discard 'camera' does not lead to an error.

Using pop() Method

The pop() method removes and returns an arbitrary element from the set. It's like reaching into the set and pulling out a surprise. However, if the set is empty, it will raise a KeyError.

# Initialize a set
colors = {'red', 'green', 'blue'}
# Pop an element
popped_color = colors.pop()
print(popped_color)
print(colors)  # The set will have one less element


In this case, pop() removes a random element from the colors set, which is then printed. The remaining set has one fewer element.

Practical Example: Cleaning a Dataset

Consider a scenario where you need to clean a dataset represented as a set by removing specific unwanted values.

  • Python

Python

# Dataset with some unwanted elements

data_set = {10, 20, 30, 40, 999}

# Unwanted elements to be removed

unwanted = {999, 40}

# Remove unwanted elements

for item in unwanted:

   data_set.discard(item)  # Using discard to avoid errors

print(data_set) 

Output

Output

In this example, discard() is used to remove unwanted elements from data_set safely, ensuring that the operation does not result in an error even if some elements are not present.

Union Operation on Python Sets

The union operation is a cornerstone of set theory in mathematics, and Python sets bring this concept to life in a practical, easy-to-use way. It's all about combining elements from two or more sets into a new set that contains all the elements from the combined sets, but, true to the nature of sets, without any duplicates. This is incredibly useful for tasks like merging datasets, combining features, or gathering distinct elements from multiple sources. Let's explore how to perform a union operation in Python and see some examples to illustrate the concept.

Using union() Method

The union() method is the formal way to perform a union operation. It takes one or more sets (or any iterables, really) and returns a new set containing all unique elements.

  • Python

Python

# Initialize two sets

set1 = {'apple', 'banana', 'cherry'}

set2 = {'google', 'microsoft', 'apple'}

# Perform a union operation

set3 = set1.union(set2)

print(set3) 

Output

{'banana', 'cherry', 'apple', 'google', 'microsoft'}


In this example, set3 is the result of the union of set1 and set2, containing all unique elements from both sets. Notice how 'apple', the common element, appears only once in set3.

Using | Operator

Python also supports the use of the pipe symbol | as a shorthand for the union operation. It's more concise and often preferred for its readability.

# Perform a union operation using the | operator
set4 = set1 | set2
print(set4)  


Output

{'banana', 'cherry', 'apple', 'google', 'microsoft'}

The result, set4, is identical to set3, showcasing the flexibility Python offers for performing union operations.

Practical Example: Merging Two Datasets

Imagine working with two datasets, each represented as a set of unique identifiers. Merging these datasets to find the total unique identifiers can be efficiently achieved using a union operation.

  • Python

Python

# Dataset 1 with unique identifiers

dataset1 = {101, 102, 103, 104}

# Dataset 2 with unique identifiers

dataset2 = {103, 104, 105, 106}

# Merge datasets to find all unique identifiers

merged_dataset = dataset1 | dataset2

print(merged_dataset) 

Output

{101, 102, 103, 104, 105, 106}


In this scenario, the union operation merges dataset1 and dataset2, resulting in a new set merged_dataset that includes all unique identifiers from both datasets without duplicates.

Set Difference in Python

The set difference operation in Python is a fundamental concept that enables you to find elements that are present in one set but not in another. This operation is akin to subtracting one set from another, and it's particularly useful when you need to identify unique elements or exclude certain items from a dataset.

Using difference() Method

The difference() method is used to perform the set difference operation. It takes another set as an argument and returns a new set containing elements that are only in the first set and not in the second.

  • Python

Python

# Initialize two sets

set1 = {'apple', 'banana', 'cherry', 'date'}

set2 = {'cherry', 'date', 'fig', 'grape'}

# Perform a set difference operation

unique_set1 = set1.difference(set2)

print(unique_set1) 

Output

{'banana', 'apple'}


In this example, unique_set1 contains elements that are exclusive to set1, effectively excluding any common elements with set2.

Using - Operator

Python sets also support the - operator to perform set differences, providing a more concise syntax.

# Perform a set difference operation using the - operator
unique_set2 = set1 - set2
print(unique_set2)  


Output

{'banana', 'apple'}

The result, unique_set2, is the same as unique_set1, demonstrating the simplicity and readability of using the - operator for set differences.

Practical Example: Filtering a Dataset

Consider a scenario where you have a dataset of all registered users and another dataset of users who have opted out of a service. To find the active users, you can use the set difference operation.

  • Python

Python

# Set of all registered users

registered_users = {'user1', 'user2', 'user3', 'user4'}

# Set of users who have opted out

opted_out_users = {'user2', 'user4'}

# Find active users by excluding opted-out users

active_users = registered_users - opted_out_users

print(active_users) 

Output

{'user1', 'user3'}


This example illustrates how the set difference operation can be effectively used to filter out specific elements from a set, in this case, identifying the active users by excluding the opted-out users.

Clearing Python Sets

Clearing a Python set is like erasing a whiteboard; you're removing all the elements from the set, leaving it empty and ready to be used again. This is particularly useful when you need to reset a set during data processing or in scenarios where you're reusing the same set for different operations in your code.

Using the clear() Method

The clear() method is the tool you'll use to wipe a set clean of its elements. It transforms the set into an empty set, effectively removing all its contents.

  • Python

Python

# Initialize a set with some fruits

fruits = {'apple', 'banana', 'cherry'}

# Clear the set using clear() method

fruits.clear()

# Check the contents of the set

print(fruits) 

 Output

set()


After calling fruits.clear(), the fruits set is empty, demonstrated by the output set(), indicating an empty set.

Why Clear a Set?

Clearing a set can be handy in various scenarios, such as:

  • Resetting Data: In iterative algorithms or loops, you might need to reset a set to an empty state before each iteration.
     
  • Reusing Variables: When you're working with limited memory or want to maintain clean and efficient code, reusing variables, including sets, is a common practice. Clearing a set allows you to repurpose it without creating a new set object.
     
  • Avoiding Data Leakage: In applications where data confidentiality is crucial, clearing sets and other data structures after their use helps prevent unintended data exposure.

Example: Using a Set in a Loop

Imagine a scenario where you're processing batches of data, and for each batch, you need to collect unique identifiers into a set. Clearing the set at the end of each batch's processing ensures that the next batch starts with a clean slate.

# Sample data: List of batches with identifiers
data_batches = [['id1', 'id2', 'id3'], ['id3', 'id4', 'id5'], ['id1', 'id2']]
# Process each batch
for batch in data_batches:
    unique_ids = set()
     # Add identifiers from the batch to the set
    for identifier in batch:
        unique_ids.add(identifier)
      # Process the unique identifiers
    print(f"Processing batch with unique IDs: {unique_ids}")
     # Clear the set for the next batch
    unique_ids.clear()


# Output will show unique IDs processed for each batch, then the set is cleared

In this example, the set unique_ids is cleared at the end of each batch processing, ensuring that each batch starts with an empty set. This illustrates the practical utility of the clear() method in managing and resetting sets within iterative processes or loops.

Frequently Asked Questions

Can I store lists or dictionaries inside a Python set?

No, you cannot store lists or dictionaries in a set because they are mutable and not hashable. Use tuples instead, as they are immutable and can be added to a set.

How do I remove duplicates from a list in Python?

Convert the list to a set using set(your_list), which automatically removes duplicates. Convert it back to a list with list(your_set) if you need a list format.

Is the order of elements preserved in a Python set?

No, sets in Python do not preserve the order of elements. They are unordered collections, meaning the order in which elements are added is not necessarily how they will be stored or iterated over.

Conclusion

Python sets offer a powerful way to handle collections of unique items. They are ideal for performing set operations like unions, intersections, and differences, making them invaluable for tasks involving distinct elements. Whether you're adding elements, clearing sets, or leveraging their time-efficient operations, sets in Python provide a robust toolkit for managing data without duplicates. Remember, sets are not just about storing; they're about optimizing your workflows with their unique characteristics and operations.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
range vs xrange in Python
Next article
Recursion in Python
Live masterclass