Sets in Python

We have discussed sequences or collections in Python in the previous posts. They are a basic data structure in Python that are used to group data together. 

We will discuss another type of sequences available in Python today called Sets. A set is a sequence of objects which is unordered and unindexed. Their values are also unchangeable which we will discuss later on.

Creating Sets

Sets are denoted by curly brackets in Python. To create a set, you just have to write comma-separated values and store it in a variable.

mySet = {“Pakistan”, “Berlin”, “Iran”, “China”, “Palestine”}
print(mySet)

When the above code is executed, it produces the following result:

{'Pakistan', 'Berlin', 'Iran', 'China', 'Palestine'}

Note: Whenever you print sets, the order of the elements will differ as they are unordered.

Accessing Elements in Sets

You cannot access elements of a set by using indices as the elements of a set are unordered and don’t have indices attached to them. However, you can use a set in a for loop to use its values.

Considering the above set, we can do:

for i in mySet:
  print(i)

When the above code is executed, it produces the following result:

‘Pakistan’
‘Berlin’
‘Iran’
‘China’
‘Palestine’

You can also use the in keyword to check if a value is present in a set.

print(“America” in mySet)

When the above code is executed, it produces the following result:

False

Modifying Sets

The elements of a set are immutable while you can add more values to a set. To add a value to a set, the add method is used.

mySet.add(“Moscow”)
print(mySet)

When the above code is executed, it produces the following result:

{'Pakistan', 'Iran', 'Berlin', ‘Moscow’,'Palestine', 'China'}

To add multiple values or another set to a set add the set time, use the update method.

mySet.update({“America”, “Australia”, “Africa”}) #you can also    
                                                 provide a list here
print(mySet)

When the above code is executed, it produces the following result:

{‘Moscow’, 'Pakistan', ‘America’, ‘Berlin’, 'Iran', ‘Australia’, 'China', 'Palestine', ‘Africa’}

Deleting Sets and Set Elements

Deleting sets is simple like all other sequences, just use the del keyword with the name of the set and it will be deleted.

del mySet

However, to delete the elements of a set, you can use the method remove.

mySet.remove(“Moscow”)
print(mySet)

When the above code is executed, it produces the following result:

{'Pakistan', ‘America’, ‘Berlin’, 'Iran', ‘Australia’, 'China', 'Palestine', ‘Africa’}

remove raises an error if the element is not present in the set. If you are not sure if an item is in a list or not, you can use discard. It will delete it if it is there and raise no error if the value is not found.

mySet.discard(“America”)
print(mySet)

When the above code is executed, it produces the following result:

{'Pakistan', ‘Berlin’, 'Iran', ‘Australia’, 'China', 'Palestine', ‘Africa’}

This was all about creating and performing different operations on Sets in Python. If you have any questions regarding the functionalities, do ask in the comments below.

Imad

I am a Software Engineer with ample experience in making games, websites, mobile apps and augmented reality solutions.

Pin It on Pinterest