Skip to content

Python Set: Everything You Need To Know

Python Set

Python sets are a data structure that allows you to store multiple values in a single variable. This can be helpful when you want to keep track of a list of items, or when you need to store data in a specific order.

In this blog post, we will discuss the basics of working with Python sets. We will cover how to create sets, add and remove items, and search for specific values.


Python Set

A Python set is a collection of unique items. It is an unordered collection, which means that the order of the items in a set doesn’t matter. Sets are created using the set() function, and you can add or remove items from a set as needed.

Sets are very useful for storing data that you need to be able to access quickly. For example, if you were creating a set of all the countries in the world, you would not want to store them in an ordered list. Sets are also helpful for finding out whether or not an item is in a collection.

Create a set

To create a set, you use the set() function and pass in the items that you want to add to the set. For example:

countries = set(["Italy", "France", "Spain", "Germany"])

Empty Set

You can also create an empty set using the following syntax:

empty_set = set()

<h3>Duplicate items</h3>
If you try to create a set with duplicate items, only the unique items will be added to the set.

For example:

duplicate_set = set(["a", "b", "c", "a"])

print(duplicate_set) # Prints: {'a', 'c', 'b'}

Remove() and add()

You can add or remove items from a set using the .add() and .remove() methods.

For example:

countries = set(["Italy", "France", "Spain", "Germany"])

countries.add("Australia") # Adds Australia to the set

countries.remove("Germany") # Removes Germany from the set


You can also use the .update() method to add multiple items to a set at once.

countries = set(["Italy", "France", "Spain", "Germany"])

countries.update(["Australia", "Canada", "Mexico"]) # Adds multiple items to the set

To check if an item is in a set, you can use the in operator.

For example:

countries = set(["Italy", "France", "Spain", "Germany"])

print("Italy" in countries) # Prints True

print("Japan" in countries) # Prints False

issubet() and issuperset()

You can also use the .issubset() and .issuperset() methods to check if one set is a subset or superset of another.

For example:

countries = set(["Italy", "France", "Spain", "Germany"])

subset = set(["France", "Spain"])

superset = set(["Australia", "Canada", "Mexico"] + countries) # Adds multiple items to the set

print(subset.issubset(countries)) # Prints True

print(superset.issuperset(countries)) # Prints True

Finally, you can use the .union() and .intersection() methods to find the union or intersection of two sets.

For example:

countries_a = set(["Italy", "France", "Spain"])

countries_b = set(["Germany", "France", "Austria"])

print(countries_a.union(countries_b)) # Prints {'Austria', 'Italy', 'Germany', 'Spain', 'France'}

print(countries_a.intersection(countries_b)) # Prints {'France'}

Sets are a very powerful data structure that can be used in a variety of ways.

Read more: Python Dictionary: A Comprehensive Guide

Leave a Reply

Your email address will not be published. Required fields are marked *