Python Data Structures (Set)

Selin Yazıcıoğlu
4 min readJul 10, 2022

--

Hello guys! I will mention the set which is data structures in this article. I will code on Visual Studio Code. Okay, let’s start!

Set

There are many features in the set. These are,

  • They are defined in {} brackets.
  • It can be changed.
  • They are not sequential. Cannot be accessed with an index.
  • It is inclusive. It can contain different types of data.
  • They cannot migrate duplicate data. Two elements with the same value cannot be found.

Give an example of the set,

int_set = {1,2,3,4}
string_set = {"Angelia", "Marwa", "Anthony"}
mix_set = {"Python_lab", 2022, True, 3.14}
print(int_set)
print(string_set)
print(mix_set)
Output:
{1, 2, 3, 4}
{'Anthony', 'Marwa', 'Angelia'}
{'Python_lab', 3.14, 2022, True}

Another example is,

my_list = [1,3,4,2,1,3,4,2,5,5,4,3]
print(my_list)
#Deleting duplicate data with set
my_set = set(my_list)
print(my_set)
Output:
[1, 3, 4, 2, 1, 3, 4, 2, 5, 5, 4, 3]
{1, 2, 3, 4, 5}

Create an Empty Set

I would like to show you how you can create an empty set.

my_set = {} #create an empty dictionary
print(type(my_set))
empty_set = set() #create an empty set
print(type(empty_set))
Output:
<class 'dict'>
<class 'set'>

Access to set elements

movies = {"The Office", "The Bing Bang Theory", "Person of Interest"}for item in movies:
print(item)
Output:
The Office
Person of Interest
The Bing Bang Theory

You can also find the length of the set as with any other data structure.

For example,

print(len(movies))
Output:
3

Adding Elements

  • add() method: This method adds a new element to the set.
  • update() method: This method adds multiple elements to the set.

add() method:

books = {"The Little Mermaid", "Notre Dame de Paris", "Alchemist", "Harry Potter"}books.add("Twilight")
print(books)
Output:
{'Notre Dame de Paris', 'Alchemist', 'The Little Mermaid', 'Harry Potter', 'Twilight'}

update() method:

books.update(["The Chronicles of Narnia", "1984"])
print(books)
Output:
{'Notre Dame de Paris', 'Alchemist', 'The Chronicles of Narnia', 'The Little Mermaid', 'Harry Potter', '1984', 'Twilight'}

Deleting Elements

There are five methods.

  • remove()
  • discard()
  • pop()
  • clear()
  • del set

remove()

This method takes a single element as an argument and removes it from the set.

# remove()
my_set = {1,2,3,4,5,6,7,8,9,0,10}
my_set.remove(2)
print(my_set)
Output:
{0, 1, 3, 4, 5, 6, 7, 8, 9, 10}

discard()

This method removes the specified item from the set. This method is different from the remove() method because the remove() method will raise an error if the specified item does not exist, and the discard() method will not.

# discard()
my_set.discard(3)
print(my_set)
Output:
{0, 1, 4, 5, 6, 7, 8, 9, 10}

pop()

This method removes a random item from the set. This method returns the removed item.

# pop()
my_set = {1,2,3,4,5,6,7,8,9,0,10}
my_set.pop()
print(my_set)
Output:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

clear()

This method removes all elements in a set. It used to give an empty output.

# clear()
my_set.clear()
print(my_set)
Output:
set()

del set

This method removes all set.

# del()
del my_set
print(my_set)

Operators

There are four methods.

  • union() -> |
  • intersection() -> &
  • difference() -> -
  • symmetric_difference() -> ^

union() -> |

This method returns a set that contains all items from the original set, and all items from the specified set(s).

my_set1 = {"Apple", "Banana", "Watermelon", "Blueberry"}
my_set2 = {"Strawberry", "Orange", "Lemon"}
my_set3 = my_set1 | my_set2
print(my_set3)
# my_set3 = my_set1.union(my_set2)
Output:
{'Orange', 'Lemon', 'Banana', 'Watermelon', 'Strawberry', 'Blueberry', 'Apple'}

You can also use it to | sign for combine sets.

intersection() -> &

This method returns a set that contains the similarity between two or more sets.

my_set1 = {1,2,3,4,5}
my_set2 = {4,5,6,7,8}
new_set = my_set1 & my_set2
print(new_set)
new_set = my_set1.intersection(my_set2)
print(new_set)
Output:
{4, 5}
{4, 5}

You can also use it to & sign.

difference() -> -

This method returns a set that contains the difference between two sets.

my_set1 = {1,2,3,4,5}
my_set2 = {4,5,6,7,8}
new_set = my_set1 - my_set2
print(new_set)
new_set = my_set1.difference(my_set2)
print(new_set)
Output:
{1, 2, 3}
{1, 2, 3}

You can also use it to - sign.

symmetric_difference() -> ^

This method returns a set that contains all items from both set, but not the items that are present in both sets.

I want to show you with an image.

I got this image from Data Science Parichay site.
my_set1 = {1,2,3,4,5}
my_set2 = {4,5,6,7,8}
unique_set = my_set1 ^ my_set2
print(unique_set)
unique_set2 = my_set1.symmetric_difference(my_set2)
print(unique_set2)
Output:
{1, 2, 3, 6, 7, 8}
{1, 2, 3, 6, 7, 8}

Other methods are,

  • isdisjoint()
  • isdissuperset()
  • isdissubsent()

isdisjoint()

This method asks “ Is the intersection of two sets empty or not?”

my_set = {3, "Python", 4, 7, True}
new_set = {"Python"}
print(my_set.isdisjoint(new_set))
Output:
False

isdissuperset()

This method asks “Does one set include another set?”

isdissubsent()

This method asks “Is one set a subset of another set?”

my_set = {3, "Python", 4, 7, True}
sub_set = {"Python"}
print(sub_set.issubset(my_set))
print(my_set.issuperset(sub_set))
print(sub_set.issuperset(my_set))
print(my_set.issubset(sub_set))
Output:
True
True
False
False

In addition,

frozenset()

This function returns an unchangeable frozenset object (which is like a set object, only unchangeable).

name_set = {"Rose", "Salmon", "Micheal"}
frozen_set = frozenset(name_set)
frozen_set.add("Jack")
Output:
---------------------------------------------------------------------------

AttributeError Traceback (most recent call last)
Input In [24], in <cell line: 4>()
1 name_set = {"Rose", "Salmon", "Micheal"}
2 frozen_set = frozenset(name_set)
----> 4 frozen_set.add("Jack")

AttributeError: 'frozenset' object has no attribute 'add'

I hope while you’re reading my article you can enjoy it!

See you on another topic :)

If you want, you can follow me at these links:

--

--