Home
About Us Privacy Policy
 

SETS IN PYTHON

ADVERTISEMENT

Intermediate Sets



What are Sets in Python, and why are they used?

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.



How to declare a set?

In Python, you can declare a set using the following methods:

  • curly "{}" brackets
  • in-built set() method. The in-built set() method takes an iterator as an argument.

Below are some examples:

__set_a = {1, 2, 3, 4, "Hello, World!", 5.0}
__set_b = set((1, 2, 3, 4, "Hello, World!", 5.0))
__set_c = set([1, 2, 3, 4, "Hello, World!", 5.0])

print(__set_a)
print(__set_b)
print(__set_c)

{1, 2, 3, 4, 5.0, 'Hello, World!'}
{1, 2, 3, 4, 5.0, 'Hello, World!'}
{1, 2, 3, 4, 5.0, 'Hello, World!'}


Data Types supported by Sets.

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:

  • list
  • dictionary
  • set


Getting the length of the set

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:

  • string
  • lust
  • tuple
  • sets
  • dictionary

Below is an example:

__a_set_example = {1, 2, 3, 4, (5, 6, 7, 8), "Hello, World", 0.007}
print(f"There are {len(__a_set_example)} items in this set.")

There are 7 items in this set.


Accessing the items in a 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.

__my_fav_colors = {
    "red",
    "green",
    "blue",
    "purple",
    "orange"
}

for color in __my_fav_colors:
    print(color)

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.


ADVERTISEMENT

Why are they unindexable?

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:

__a_set_example = {(0,), (5, 6, 7, 8), 1, "Hello, World", 0.007}
print(__a_set_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.


Add Items in a Set using the add() method.

The set allows for adding of values using the creatively named method add(). Below is an example

__my_fav_set_in_python = {
    1, 
    2,
    3,
}

__my_fav_set_in_python.add(4)
print(__my_fav_set_in_python)

{1, 2, 3, 4}


Is adding duplicate values in sets possible?

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.

__my_fav_set_in_python = {
    1, 
    2,
    3,
}

__my_fav_set_in_python.add(4)
print(__my_fav_set_in_python)

# Now adding the value of 4 again
__my_fav_set_in_python.add(4)
print(__my_fav_set_in_python)

{1, 2, 3, 4}
{1, 2, 3, 4}

As you can observe from the output, re-adding the value didn't do any effect.


Adding another set using the update() method

Sets allow for adding another set using the in-built update() method. Below is an example.

a_set = {1, 2} 
another_set = {3, 4} 
a_set.update(another_set) 
print(a_set)

{1, 2, 3, 4}


Adding all the items of iterable in a set.

Sets also allow for adding all the valid items in an iterable in a set.

a_set = set()

# Adding a List
a_set.update([0, 1, 2, 3])

# Adding a Tuple
a_set.update((4, 5, 6, 7))
print(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.

a_set = set()
a_set.update(
    [
        [1],   # List, invalid
        2,   # Int, valid
        3,   # Int, valid
        4    # Int, valid
    ]
)
print(a_set)

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:

  • Dictionary
  • Set
  • List

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.


ADVERTISEMENT

Removing an Item from the set

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:

  • remove()
  • discard()

a_set = {31, 332, 34, 48, 53, 16, 731, 83, 9, 100}
print(a_set)

# Using the remove() method
a_set.remove(31)
print(a_set)

# Using the discard() method
a_set.discard(332)

print(a_set)

{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}


Removing the last item from the set using the pop() method.

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.

a_set = {31, 332, 34, 48, 53, 16, 731, 83, 9, 100}
print(a_set)

# Using the remove() method
a_set.remove(31)
print(a_set)

# Using the discard() method
a_set.discard(332)

print(a_set)

{34, 100, 9, 332, 48, 16, 83, 53, 731, 31}
{100, 9, 332, 48, 16, 83, 53, 731, 31}
The item removed was 34


Clearing the set using the clear() method

During development, you could need to empty the set for various reasons. Python provides the clear() method to do that.

a_set = {31, 332, 34, 48, 53, 16, 731, 83, 9, 100}
a_set.clear()
print(a_set)

set()


Joining two sets

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.

set_1 = {1, 2, 3, 4}
set_2 = {1, 8, 9}
set_3 = set_1.union(set_2)
print(set_3)

{1, 2, 3, 4, 8, 9}

The union() method will remove any duplicate items.


Methods
add()

The in-built add() method will add a valid object to the set. Valid objects are anything but the following:

  • List
  • Set
  • Dictionary

Below is an example:

__my_fav_set__ ={10, 300, 201011}
__my_fav_set__.add(191991)
print(__my_fav_set__)

{10, 201011, 300, 191991}


clear()

The in-built clear() method will clear the contents of the set. Example is as follows:

__my_fav_set__ = {10, 300, 201011}
__my_fav_set__.clear()
print(__my_fav_set__)

set()


copy()

The in-built copy() method returns a duplicate of the set.

__my_fav_set__ = {10, 300, 201011}
__set_copy = __my_fav_set__.copy()
print(__set_copy)

{10, 201011, 300}


difference()

The in-built difference() method returns the difference between two or more sets.

x = {1, 32, 33} 
y = {32, 33, 782, 53, 443, 5276, 576} 
z = x.difference(y) 
print(z)

{1}


difference_update()

The in-built difference_update() will remove the items that are common to both sets. Below is an example:

x = {1, 32, 33} 
y = {32, 33, 782, 53, 443, 5276, 576} 
x.difference_update(y) 
print(x)

{1}


discard()

This in-built discard() method will remove the specified item from the set.

x = {1, 32, 33} 
x.discard(32)
print(x)

{1, 33}


intersection()

This in-built intersection()method will return the items shared in both sets.

x = {1, 32, 33, 34, 35, 36, 37} 
y = {1, 35}
z = x.intersection(y)
print(z)

{1, 35}

intersection_update()

This in-built intersection_update() method will remove the items not present in the other set.

x = {1, 2, 3}
y = {2, 3, 4, 5, 6}
x.intersection_update(y)
print(x)

{2, 3}


isdisjoint()

This in-built isdisjoint() method will return whether the two sets have an intersection or not.

x = {1, 221, 34543} 
y = {221, 1, 3, 4, 5, 6} 
print(x.isdisjoint(y))

False


issubset()

This in-built issubset() method will return whether the set contains another set.

x = {1, 2, 3}
y = {2, 3, 4, 5, 6, 1, 3}
print(x.issubset(y))

True


issuperset()

This in-built issuperset() method will return whether the "super" set contains another set.

x = {149, 2392, 321, 466, 522, 149, 2392, 321, 466, 2, 3, 4, 5, 6, 1, 3, 216, 111}
y = {149, 2392, 321, 466, 2, 3, 4, 5, 6, 1, 3}
print(x.issuperset(y))

True


pop()

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:

x = {1, 2, 3} 
x.pop() 
print(x)

{2, 3}


remove()

This in-built remove() method will remove the item specified. Below is an example:

x = {-1, 1, 2, 3, 92, 2911, 32022, 0, "Hello, World"} 
x.remove(2) 
print(x)

{0, 1, 3, 'Hello, World', 32022, 92, -1, 2911}

The above output could be different from session to session.


symmetric_difference()

This in-built symmetric_difference() method will return the difference between the two sets.

x = {1, 2, 3} 
y = {2, 5, 6} 
z = x.symmetric_difference(y) 
print(z)

{1, 3, 5, 6}


symmetric_difference_update()

This in-built symmetric_difference_update() method will update the set by removing all the items common to both sets.

x = {1, 2, 3} 
y = {2, 5, 6} 
x.symmetric_difference_update(y) 
print(x)

{1, 3, 5, 6}


union()

This in-built union() method will return a new containing unique elements from multiple sets.

x = {1, 2, 3} 
y = {2, 5, 6} 
z = x.union(y) 
print(z)

{1, 2, 3, 5, 6}


update()

This in-built update() method will add unique elements from the other set.

x = {1, 2, 3} 
y = {4, 5, 6} 
x.update(y) 
print(x)

{1, 2, 3, 4, 5, 6}


ADVERTISEMENT

Conclusion

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.


ADVERTISEMENT



All product names, logos, and brands are property of their respective owners.