Python Set and Tricks By albro

Python Set and Tricks

Sets are used in Python to store data. A set is a data type similar to a list that can hold data of the same type or different types. The two main differences of sets are the non-ordering of elements and the non-duplicate of its members. In this post, you will get to know sets and tricks for working with Python sets.

If I want to introduce python sets very simply, I will refer you to set theory in mathematics! Surely you have met sets in math class. If you don't have the presence of mind right now, there is nothing to worry about.

In sets we store a group of elements. New members can be added to any set or old members can be removed, but existing members cannot be changed. Also, different operators are defined between the two sets; Operators such as union, intersection, difference.

Python Sets Venn Example

Sets are used for exactly the same purpose in Python programming. First, let's get acquainted with set features in Python and learn how to define it. Then I'll introduce the main functions of working with the set.

Set in Python

Set is a built-in data structure. The members of a Python set have the following three main properties:

  • unordered
  • unchangeable
  • non-duplicate

Unordered means that no particular order can be considered for the elements in the set. In addition, unlike the list in Python, the set does not have an index. As a result, one of its members cannot be accessed by index.

The members of a set are unchangeable; Therefore, a value cannot be changed. In fact, since we cannot access a particular element, we cannot change it.

All members of a set are unique in Python. That is, there are no two members of the same set that are equal. We will see further that if we try to have duplicate values while defining the set, automatically only one of them will remain.

Python set definition

We have two types of definition structures for set in Python. We use the first mode when we have at least one value of the set. The brace symbol is used to define the set. We start the set with { and end it with }. Each member in the set is also separated from each other using a comma (,).

num_set = {17, 81, 4, 11}
hive_set = {"albro", "ocdb", "xeldal"}

We cannot use {} alone to define a set. Unlike the lists that were defined by putting [], if we only use Accolade, we will have a Python dictionary. Pay attention to the following code:

my_set = {17, 81, 4, 11}
print( type(my_set) )   # <class 'set'>

test = {} print( type(test) ) # <class 'dict'>

The second way to define a set in Python is to use the set() constructor. By calling this function, an empty set will be created to which we will be able to add new values with the help of the functions that we will see in the next section.

X = set()

The set() function can receive an iterator dataset as input in Python. Iterator data is data that can be traversed. So we can give a list, tuple or string as input to this function and convert it to set.

lst = ["albro", "ocdb", "xeldal", "mightpossibly"]
accs = set(lst)
print(accs)
# output: # {'albro', 'ocdb', 'xeldal', 'mightpossibly'}

Instead of a list, you can give a text string to set() and get a set of unique characters of the desired string.

site_name = "leofinance.io"
site_chars = set(site_name)
print(site_chars)
# output: # {'n', 'i', 'c', 'o', '.', 'a', 'e', 'l', 'f'}

Note that in the example above, some characters such as o, which are used several times in the string, are included only once in the set.

I remind you that the main features of the set in Python (Python Set) are unordered, unchangeable and non-duplicate. This data type is based on the Hash Table data structure.

Working with Python sets

The data we put in a set can be of the same type or different types. For example, in the following code snippet, you can see that three different sets are created with different data.

my_set1 = {6, 17, 28}
my_set2 = {"albro", 2557, "Hive", (2,5)}
my_set3 = {2.4, "Hello", (4,7,9)}

I said that set members are immutable in Python. This means that the value of an element in the set cannot be changed. But note that set is mutable in Python. That means we can add new members or remove some members. In the following, we will get to know the functions that will do such things.

I remind you that we cannot directly access the members in the set through index.

Usually, one of the things we do with datasets is to calculate their size. Size refers to the number of members in a set. To calculate the size of the set in Python, we use the len() function.

It is enough to give set as input to this function to have the number of members of the set.

hive_set = {"albro", "xeldal", "ocdb", "ecency", "leo.voter"}
print( len(hive_set) )
# output: # 5

Checking the existence of the set member

Another common thing we do with sets is to check if a value exists in the set. This process is usually done in Python's conditional statements to make a decision.

To check the existence of a value in the set, we use the in keyword. Consider the following example:

print( "albro" in hive_set )
# output:
# True
test_set = {1, 2, 3, 5, 8} if 7 in test_set: print("Yes!") else: print("No!")

In the second part of this code, after defining the set test_set, we have checked in an if condition whether the value 7 is in the set or not. By running the code, the value of No! will be printed.

In order to check the absence of an element in the set, we will use the expression not in. In the following code, we have checked the absence of themarkymark in the set:

names_set = {"albro", "xeldal", "ocdb", "ecency", "leo.voter", "xeldal"}
if "themarkymark" not in names_set:
    print("OK!")

Question: In the definition of this set, the name xeldal is repeated twice. If we print the value of names, we will see that the output is similar to the image below. Why?!

result print name_set

tricks

Add member to set

We said that unlike set elements, sets themselves can be changed. To add a new member to the set in Python, we use the add() method. This method is defined as a function on the set data type.

So to use it, we need to call the method on the desired set. This method takes an input that specifies the value of the new element.

test_set.add(11)
# {1, 2, 3, 5, 8, 11}

Sometimes we need to add some new members from a list to our set. The first method is to use a for loop to add all elements of the list to the set.

The second and better method is to use the update() method. This method takes a list as input and adds all its members to our set.

new_nums = [9, 15, 13]
test_set.update(new_nums)
print( test_set ) # {1, 2, 3, 5, 8, 9, 11, 13, 15}

Delete from Python set

To remove an element from the set in Python, discard() and remove() methods are used. These two methods take a value as an input and remove it from the set we want.

These two methods have only one difference: if the desired value does not exist in the set, discard() does nothing, but remove() throws an error.

test_set.remove(8)
# {1, 2, 3, 5, 9, 11, 13, 15}
test_set.remove(7) # Error!
test_set.discard(7) # {1, 2, 3, 5, 9, 11, 13, 15}

Set operators in Python

If you remember from math class, we used to do different operations between sets. Sometimes we need to do some of those things in Python. In the following, we examine 3 commonly used operators.

Here, I use numerical elements to make the examples easier to understand. But the data types of sets can be anything. Consider two sets A and B:

A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

Union between two sets in Python

The union of two sets is a set of all members of both sets. For union on sets in Python from operator | Or the union() method is used.

Operator | It is placed between two sets and similar to mathematical operators, it will give us the final result. The union() method is called on one of the sets and the other set is given as input.

A_union_B = A | B
# A_union_B = {1, 2, 3, 4, 5, 6, 7, 8}
print( A.union(B) ) # {1, 2, 3, 4, 5, 6, 7, 8}

Set Union Venn Diagram

Difference between two sets in Python

The difference of a set B from A is the set of all members that are only in A. In other words, members of A who are not present in B.

To calculate the difference between two sets, the subtraction operator (-) or the difference() method is used. Consider the following example:

print( A - B )
# print( A - B )
print( A.difference(B) ) # {1, 2, 3}
print( B.difference(A) ) # {8, 6, 7}

Set Difference Venn Diagram

Note that in the general case, the set A-B is not equal to B-A.

Set intersection

The intersection of two sets is a set of members that are present in both sets. If two sets do not have any elements in common, their intersection will be empty.

To perform intersection operations on two sets in Python, the & operator or the intersection() method is used.

print( A & B )
# {4, 5}
print( A.intersection(B) ) # {4, 5}

Set Intersection Venn Diagram

Summary: Set in Python

Sets are used in Python to store data. A set is a data type similar to a list that can hold data of the same type or different types. The two main differences of sets are the non-ordering of elements and the non-duplicate of its members.

In this post, I discussed how to make a set in Python, different rules and methods of working with it. We have seen that sets can be easily used.

I talked about actions such as union, difference, and intersection, and explained how members can be added or removed from sets. In addition, I also talked about some errors that you may encounter.

I hope this post has been of interest to you. If you know a trick about working with sets that is not mentioned or if you have a comment about this post, the comments section is for you.