Python Set

Python Set is similar to Python List, the difference is set items are unchangeable, but you can add or remove item and Set not allow duplicate item. This section of Python tutorial will show all the operation of Set.


Objectives

  • Python Set Syntax

  • Create and assign value in Set

  • Python Set Access elements

  • Set data manipulations

  • Python Set all methods and it’s task


Python Set Syntax

set_data = {"value", "other value"}
Here
  • Once a set is created, we cannot change its items, but you can add new items and can remove as well.

  • Duplicates Not Allowed

  • Unordered


Create and assign value in Python Set

# Define a list
set_data = {}

# Assign value to a set
set_data = {"Touhid Mia", 28, True, 28.5}


Python Set Access elements

This part you are going to learn access set elements, let’s jump into the codes.


Access element by index

Code

# Declare Set for access data element
set_data = {"Touhid Mia", "Bangladesh", 28, 5.10, True}

# Print each element of list by loop
for item in set_data:
    print("Item : " + str(item))

Output

Item : True
Item : Bangladesh
Item : 5.1
Item : Touhid Mia
Item : 28

Process finished with exit code 0

Note : Set not allow accessing item using index or key, but you can loop through the set items using a for loop.


Check set value is exist on it

Code

# Declare Set for access data element
set_data = {"Touhid Mia", "Bangladesh", 28, 5.10, True}

# Check set value is exist on it
if "Bangladesh" in set_data:
    print("Yes key exist")
else:
    print("Key not exist")

Output

Yes key exist

Process finished with exit code 0


Python Set data manipulations

Earlier part you saw that how to retrieve or access element from a set, this part you will learn how to add, remove data to list


Set add item using add function

Code

# Declare Set
set_data = {"Touhid Mia", "Bangladesh", 28, 5.10, True}

# Set add item using add function
set_data.add("I have a child")
print(set_data)

Output

{True, 5.1, 'Touhid Mia', 'Bangladesh', 28, 'I have a child'}

Process finished with exit code 0


Set add item using update function

Code

# Declare Set
set_data = {"Touhid Mia", "Bangladesh", 28, 5.10, True}
updated_set_data = {"I have a child", "I have a beautiful wife"}

# Set add item using update function
set_data.update(updated_set_data)
print(set_data)

Output

{True, 5.1, 'Touhid Mia', 'Bangladesh', 'I have a beautiful wife', 28, 'I have a child'}

Process finished with exit code 0


Set remove item using remove function

Code

# Declare Set
set_data = {"Touhid Mia", "Bangladesh", 28, 5.10, True}

# Set remove item using remove function
set_data.remove("Bangladesh")
print(set_data)

Output

{True, 5.1, 28, 'Touhid Mia'}

Process finished with exit code 0


Set remove item using discard function

Code

# Declare Set
set_data = {"Touhid Mia", "Bangladesh", 28, 5.10, True}

# Set remove item using discard function
set_data.discard("Bangladesh")
print(set_data)

Output

{True, 5.1, 28, 'Touhid Mia'}

Process finished with exit code 0


Set remove last item using pop function

Code

# Declare Set
set_data = {"Touhid Mia", "Bangladesh", 28, 5.10, True}

# Set remove last item using pop function
set_data.pop()
print(set_data)

Output

{5.1, 28, 'Touhid Mia', 'Bangladesh'}

Process finished with exit code 0

Note : Set is unordered so it difficult to understand what will be the last element, so possibility very high miss interpret.


Set remove all item using clear function

Code

# Declare Set
set_data = {"Touhid Mia", "Bangladesh", 28, 5.10, True}

# Set remove all item using clear function
set_data.clear()
print(set_data)

Output

set()

Process finished with exit code 0


Python Set all methods and it’s task

  • clear() : Removes all the elements from the set

  • difference() : Returns a set containing the difference between two or more sets

  • discard() : Remove the specified item

  • intersection() : Returns a set, that is the intersection of two other sets

  • issubset() : Returns whether another set contains this set or not

  • copy() : Returns a copy of the set

  • issuperset() : Returns whether this set contains another set or not

  • pop() : Removes an element from the set

  • remove() : Removes the specified element

  • union() : Return a set containing the union of sets

  • isdisjoint() : Returns whether two sets have a intersection or not

  • difference_update() : Removes the items in this set that are also included in another, specified set

  • add() : Adds an element to the set

  • intersection_update() : Removes the items in this set that are not present in other, specified set(s)

  • update() : Update the set with the union of this set and others


Python Set video tutorial