Intermediate Sets
Sets are one of the four in-built data types supporting storing multiple items of various data types. Items stored in a set are unindexed and unordered, immutable and unique.
Items in a set are unique, unordered, and unchangeable.
In Python, you can declare a set using the following methods:
Below are some examples:
{1, 2, 3, 4, 5.0, 'Hello, World!'}
{1, 2, 3, 4, 5.0, 'Hello, World!'}
{1, 2, 3, 4, 5.0, 'Hello, World!'}
Since Python is a programming language that provides a high-level abstraction, it supports multiple data types. If you have followed this tutorial, we have covered Lists, Tuples, Dictionary and all of these data types support the value of any data type. However, sets set a restriction. It cannot store the following data types:
To find the length of the set, Python provides the in-built len() method. If you had programmed in Python before or followed this tutorial, we have been using the in-built len() method almost everywhere. The len() method is an in-built method to find the length of multiple data types such as:
Below is an example:
There are 7 items in this set.
In this tutorial, we have extensively covered indexing for lists, tuples, and dictionaries. However, values within a set are unindexable. To access sets, we must iterate through them using a loop.
red
blue
green
purple
orange
One thing to notice is that the output will differ if you run the program multiple times.
Due to having different orders of storing information every time, indexing to get a specific value is impossible and will likely yield incorrect results. More is covered in the next section.
Good question. Other Python tutorials most often ignore questions like this. Although they do a good enough job, they always lack some inner aspects of Python. However, we won't.
At the beginning we discussed that sets are unordered amongst others. Let me provide this to you with an example:
Your output might be different. But according to Python 3.8.10, here is the output:
{1, (5, 6, 7, 8), 'Hello, World', 0.007, (0,)}
As you can observe from the generated output, the tuple (0,) is declared the first item in the set; however, we get this output when printing the set.
If this were a list or a tuple, (0,) would have been the first item. However, sets are unordered. Hence, they cannot be accessed using indexes because they will provide incorrect results leading to massive application bugs, and therefore they are accessed using a loop.
The set allows for adding of values using the creatively named method add(). Below is an example
{1, 2, 3, 4}
Absolutely No. We learned about the add() method to add an item to a set in the prior section. Let us revisit that example and try to add the same item twice.
{1, 2, 3, 4}
{1, 2, 3, 4}
As you can observe from the output, re-adding the value didn't do any effect.
Sets allow for adding another set using the in-built update() method. Below is an example.
{1, 2, 3, 4}
Sets also allow for adding all the valid items in an iterable in a set.
{0, 1, 2, 3, 4, 5, 6, 7}
In the above example, we added a list and a tuple. But what if the list contains a data type not acceptable by sets? Let us try another example to find out.
Traceback (most recent call last):
File "test.py", line 2, in >module<
a_set.update(
TypeError: unhashable type: 'list'
In the example mentioned above, we are trying to add three data types that sets do not accept:
The above example will generate a TypeError when trying to execute for all three invalid data types.
Therefore, when adding an iterable, ensure that it does not contain any invalid data types.
Just as Python allows for adding an item, it allows for removing an item as well. To remove items from the set, Python provides two in-built methods:
{34, 100, 9, 332, 48, 16, 83, 53, 731, 31}
{34, 100, 9, 332, 48, 16, 83, 53, 731}
{34, 100, 9, 48, 16, 83, 53, 731}
In the prior section, we learned about removing items using the in-built methods. However, Python also provides the pop() method to remove the last item in the set. When using the pop() method, always remember that the last item might necessarily mean the last item inserted. The pop() method returns the item removed. Below is an example.
{34, 100, 9, 332, 48, 16, 83, 53, 731, 31}
{100, 9, 332, 48, 16, 83, 53, 731, 31}
The item removed was 34
During development, you could need to empty the set for various reasons. Python provides the clear() method to do that.
set()
During development, you could encounter situations where you might want to join two sets. One crucial detail to consider is that the resulting set will only contain unique elements from both sets. To join two sets, Python provides the union() method. Below is an example.
{1, 2, 3, 4, 8, 9}
The union() method will remove any duplicate items.
The in-built add() method will add a valid object to the set. Valid objects are anything but the following:
Below is an example:
{10, 201011, 300, 191991}
The in-built clear() method will clear the contents of the set. Example is as follows:
set()
The in-built copy() method returns a duplicate of the set.
{10, 201011, 300}
The in-built difference() method returns the difference between two or more sets.
{1}
The in-built difference_update() will remove the items that are common to both sets. Below is an example:
{1}
This in-built discard() method will remove the specified item from the set.
{1, 33}
This in-built intersection()method will return the items shared in both sets.
{1, 35}
This in-built intersection_update() method will remove the items not present in the other set.
{2, 3}
This in-built isdisjoint() method will return whether the two sets have an intersection or not.
False
This in-built issubset() method will return whether the set contains another set.
True
This in-built issuperset() method will return whether the "super" set contains another set.
True
This in-built pop() method will return the last element. Do not confuse this with the last element inserted, as the insertion are unordered. Below is an example:
{2, 3}
This in-built remove() method will remove the item specified. Below is an example:
{0, 1, 3, 'Hello, World', 32022, 92, -1, 2911}
The above output could be different from session to session.
This in-built symmetric_difference() method will return the difference between the two sets.
{1, 3, 5, 6}
This in-built symmetric_difference_update() method will update the set by removing all the items common to both sets.
{1, 3, 5, 6}
This in-built union() method will return a new containing unique elements from multiple sets.
{1, 2, 3, 5, 6}
This in-built update() method will add unique elements from the other set.
{1, 2, 3, 4, 5, 6}
In this chapter, we learned about the in-built data-structure "sets" and their usage, declaring a set in Python, eligible data types in a set, getting the number of elements within a set using the in-built len() method, accessing the items within a set.
Additionally, we learned about adding and removing items, joining multiple tuples, and a brief overview of several in-built methods for manipulating sets.