📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Уроки Python с нуля / #11 – Множества (set и frozenset)

Школа itProger / Программирование8:10

Transcription

Hello! In this lesson, we will study sets in Python. We will explore both the set and the frozenset.

Before we begin, I would like to recommend the website itprager.com. On this site, you will find code, homework assignments, and a lot of other useful information. The link to this lesson will be in the description of this video.

A set is actually similar to a list, but in a set, all elements are in random order, and there cannot be any duplicate elements.

So, why are sets useful? Let's say we want to get some specific data from the user. For example, we could ask for a string that lists all their hobbies. We can then split this string by a certain character using the split method, which we have already studied.

Next, I want to remove all duplicate elements from the resulting list. This can be done by converting the list into a set, which will automatically remove all duplicate elements.

First, you can create a set yourself, and it will not have duplicate elements, and all elements will be in random order. You can also convert an existing list into a set, thus removing all duplicates. Plus, if you need to shuffle the elements randomly, a set can do this quickly and easily.

Now, let's get to the practical part. To create a set, you can use two available methods. First, you can use the set function. You usually pass a certain word to this function. For example, let's pass the word "hello."

Now, let's try to print this set, and we will see that it contains only four elements. The duplicate character has been removed, and all elements are in random order.

So, the first way to create a set is by using the set function. This function is typically used when we need to quickly convert a list into a set or when we want to convert a word, which is essentially a list of characters, into a set.

However, if we want to create a set manually, we usually use curly braces. I agree that we used curly braces in the last lesson to create dictionaries, but the difference is that if we specify both keys and values in curly braces, we will always create a dictionary. If we use only values, as I am doing now, we are actually creating a set.

You can see that I added two duplicate elements here. If we run this now, we will notice that they are again displayed in random order. In this case, they happen to be in ascending order, and the duplicate elements have been removed. The last duplicate, which was the number five, has been removed from the set.

Working with sets is just like working with regular lists, except that in a set, we cannot access specific elements by their index, nor can we change them. If we try to do that, we will always get an error.

At the same time, we can always add or remove new elements. If we need to add a new element, we use the add function. Here, let's add a number, for example, 32. This number has been added to the set.

If I need to add several numbers or values at once, I can use the update function. Let's try adding different data types as well. This will work just fine. If I add duplicate data types, they will not be added, but all other elements will be added without issue.

If you need to remove a specific element, you can use the remove function. Here, you just specify the value of the element. For example, I want to remove the element with the value True. If we run this, you will see that this element no longer exists.

If you want to remove the first element, you can use the discard method. After running this, we will see that the number 32 has been removed, and it no longer exists.

If you need to clear the entire set, you can use the clear function. Let's try running this, and you will see that it simply returns an empty set, indicating that there are no elements left.

Now, let's consider a situation where we have a list consisting of duplicate elements, and we need to remove all duplicates from this list. To do this, we can place a new value directly into the same list. I will call the set function and place the list inside it.

This way, we create the first list and then remove the previous list, creating a new list. This new list will be a set without duplicate values. If you want to keep the previous list, you can name it something like new_list and then print this new list.

If we run this now, we will see that we have a set consisting of only three elements, with no duplicates. All the unnecessary duplicates have been removed.

Finally, we need to discuss frozensets, or in other words, immutable sets. Essentially, a frozenset is a combination of a tuple and a set. We create a regular set, but we cannot modify the elements inside this set. This is similar to the functionality of tuples.

To create a frozenset, we can do the following. Let's create something like new_data and then use the frozenset function, passing a list to it. To pass a list, we open square brackets and provide certain values.

Let's copy these values from the previous elements and add them to the frozenset. If we try to print this, we will see that the properties of sets apply here: the elements are mixed, and there are no duplicate values.

However, if I try to access the frozenset and use a function that doesn't exist, or if I try to add something, it will raise an error because we cannot modify frozensets.

So, we create a combination of a tuple and a set, which is called a frozenset. With sets and frozensets, we can create various sets without duplicate elements, and all elements will be displayed in a random order.

That's all for today! I hope you enjoyed the lesson. If so, don't forget to subscribe and join our social media. All the links will be in the description of this video. That's all from me. See you soon! Goodbye!