Python Sets (Everything You need to Know)

Introduction

Python Set is an unordered(It is not guaranteed, in which order the items of a set will appear each time) collection of unique items (it doesn't contain any duplicate values). It's a built-in data type in python that stores comma-separated values between two curly brackets.

In this tutorial, we will cover every single piece of information related to Python Sets.

Example

Here, 'set_var' is a Python Set Variable.

set_var = {17, 3, 2022, "I love coding", (8, 7, 9)}

Python set vs Python List

There are two major differences between Python Lists and Sets

1. Python Lists are mutable but Sets aren't immutable.

2. A Set can't contain any duplicate values but lists can.

Add data to a Python Set

You've seen the declaration of a Set Variable before. Here, we'll add data to a pre-defined Set Variable.


var = {8, 1, 2021, 'I Love Coding', (5,4,6)}
var.add('Python')
print(var)

Output

{1, 'Python', 2021, 8, 'I Love Coding', (5, 4, 6)}

Remove items from a python set

There are two methods for removing items from a Python Set. Let's see one by one.

Method 1: Using the remove() method


var = {8, 1, 2021, 'I Love Coding', (5,4,6)}
var.remove(2021)
print(var)

Output

{1, 8, 'I Love Coding', (5, 4, 6)}

Method 2: Using the discard() method

The discard() method is almost identical to the remove() method; The main difference here is that if you try to remove an item from a set that doesn't exist, the remove() method will return an error but the discard() method will return no error message.


var = {8, 1, 2021, 'I Love Coding', (5,4,6)}
var.discard(2022)
print(var)

Output

{1, 2021, 8, 'I Love Coding', (5, 4, 6)}

Remove duplicate elements from a list using Python Set

As I told you earlier, Python sets cannot store any duplicate items. Here, we will use this facility to remove duplicate elements from Python lists. We just need to follow two simple steps. These are as follows.

Steps

1. Convert the list to Set.

2. Next, convert the Set to Python List again.

That's it. Duplicate values will be automatically removed.


# List with duplicate values
py_list = [1,2,2,2,4,5,6,4,9]
print(f"Given list is: {py_list}")
# Converting list to set
py_set = set(py_list)
print(f"After converting: {py_set}")
# Converting set to list again
py_list = list(py_set)
print(f"Type of py_list is: {type(py_list)}")

Output

Given list is: [1, 2, 2, 2, 4, 5, 6, 4, 9]
After converting: {1, 2, 4, 5, 6, 9}
Type of py_list is: <class 'list'>

update() method in Python Set

You can add any type of python data(iterable: list, tuple, dictionary, and int, float, string, etc.) to a Set using the update() method.

An important message: If you pass a Python dictionary to a set, only the keys of the dictionary will be added. See the example below for a better understanding.


pyseek = {8, 1, 2021, 'I Love Coding', (5,4,6)}
py_list = [9, 5, 3.5]

pyseek.update(py_list)
print(f"First: {pyseek}")

# Add a string
py_set = {'Hello PySeek'}

pyseek.update(py_set)
print(f"Second: {pyseek}")

dict_var = {'key1': 'val1', 'key2': 'val2'}

# Adding Dictionary to the Set
pyseek.update(dict_var)
print(f"Third: {pyseek}")

Output

First: {1, 3.5, 2021, 'I Love Coding', 5, 8, (5, 4, 6), 9}
Second: {1, 3.5, 2021, 'I Love Coding', 5, 8, 9, 'Hello PySeek', (5, 4, 6)}
Third: {1, 3.5, 'key1', 2021, 'I Love Coding', 5, 8, 9, 'Hello PySeek', 'key2', (5, 4, 6)}

Summary

In this tutorial, our topic of discussion was Python Set and all its functionalities. Python Sets are unordered, immutable, and collections of unique data items. We covered several examples to cover the whole topic.

For any queries related to this topic just leave your comment below. You will get an immediate response.

Thanks for reading!💙

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post