Learn about Python Set

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 items). 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 information related to Python Sets.

Example

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

Python set vs Python List

There are two major differences between these two data types in python.

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 Python Set

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 them 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

Here, we will use the discard() method to remove items from a set. This 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.

1. Convert the list to Set

2. Next, convert the Set to List again.


# 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, 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. The below example is for you.


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}")

Output

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

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