Sets

Let us consider another type of data in Python which is called “Sets”. Like Lists, Tuples, & Dictionaries, Sets also provide us a way to store a collection of items but have their unique properties.

Let’s look at an example of a Set:

set_variable = {"Python", "Dynamically Typed", "Programming Language"}

Accessing items of a Set

Unlike a List, we cannot access items of a Set through index values. If we try to, we get the following error:

print(set_variable[0])

This should give us:

This is because a Set unlike a List or a Tuple is an Unordered collection, which does not retain the order in which the programmer enters them. To test this out let’s print our “set_variable” to see what we get:

print(set_variable)

As the order is ever-changing, the value stored in the first index will vary each time it’s called, so we cannot access its elements like a List. Thus, each time we print the set the order of items within it will vary.

We can check the items of a Set in the following manner:

  • Firstly, a looping mechanism to see all the values of a Set.
  • Secondly, to check if a required item is present within a Set.

 

With a looping mechanism, we can access its items the same way we would, with a list. But since it is unordered, each time we run it we get the items in a different order.

set_variable = {"Python", "Dynamically Typed", "Programming Language"}

for x in set_variable:
print(x)

The first time we run this we get:

If we run it again, we get:

We can also check if an item is present within a Set, in the following manner.

set_variable = {"Python", "Dynamically Typed", "Programming Language", "Python"}

if "Video Games" in set_variable:
print("It is present")
else:
print("We could not find it.")

The output will be:

Adding & Removing items to a Set

Even though we cannot modify the elements of a Set we can add or remove items from it.

Adding Items

We add items to a list using the add() function.

set_variable = {"Python", "Dynamically Typed", "Programming Language"}
set_variable.add("Test Value")

This should give us:

We can also update a Set to store the items of a Set or even a List, using the update() function.

set_variable = {"Python", "Dynamically Typed", "Programming Language"}

set_variable_1 = {"Value 1"}
list_variable = ["Value 2"]

set_variable.update(set_variable_1)
set_variable.update(list_variable)

This should give us

NOTE: After running the program a few times, Value 1 & Value 2 came simultaneously. This does not mean they come together when the code was executed.

Removing Items

We can use the remove() function or the discard() function to remove an item from our Set.

set_variable.remove("Python")

This should give us:

Python also offers the pop() function which removes the last item, but since the order changes every time, so does the last item of the Set. We can also use clear() to remove all items from the list.

Use Cases for a Set

With all of the different data types we have learned so far, it might be beneficial for us to understand when to use Sets. Sets are preferred when we want an unordered collection of unique items and the term unique is important, as Sets do not allow for duplicates.

Consider the updated set variable where we included the value “Python” twice.

set_variable = {"Python", "Dynamically Typed", "Programming Language", "Python"}

print(set_variable)

If we try printing it, we get the output as:

We see that the value Python was only found once in the Set. So, this might be helpful in situations where we only want to store unique values and we are not bothered with how the items are ordered, like a collection of selected user names for an online video game.

What have we learned?

  • What is a Set?
  • What are ordered and unordered collections?
  • What are the unique properties of a Set?
  • What functions can we use to add items to a Set?
  • How can we add items from other types of collections to a Set?
  • What functions can we use to remove items from a Set?

 

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments