Learning Python From Scratch: Data Structures — Set

Elif Çelik
CodeX

--

Hello everyone! I am one of Sisterslab’s Women in Tech Academy project participants, supported by Toplum Gönüllüleri Vakfı. The project aims to empower 25 women between the ages of 20–28 with software training for 3 months and seek their participation in the workforce in the sector. You can find more information about the project at this link: https://sisterslab.co/women-in-tech-academy/

I will try to cover python from scratch in this medium article series. In today's article, I’m going to explain the sets. You can check the other data structure or python articles from my medium account!

Photo by James Harrison on Unsplash

SETS

One of the most important features of sets is that they are unordered. So this data structure does not keep the data in indexed way. In this respect, we can say that it is a simple data structure. So we can give an example that sets are like the various items that we have in our bag. So let’s have a look at the other properties of sets.

  • They cannot be accessed with an index, they are unordered.
  • Immutable.
  • The difference from tuples is that we can add and remove new elements to the set. But we cannot change an element in the set.
  • They are defined in {} brackets. And commas separate elements.
  • May contain different types of data.
  • They cannot migrate duplicate data. Two elements with the same value cannot be found.

How To Create Sets

Like all of the other data structures, creating sets are also really easy. All you need to do is use curly brackets and separate the items with commas.

Deleting the Duplicate Data

Because there is no duplicate data, using a set data structure is really useful when we want to see the existence of items. So if you need to delete the duplicates you can convert the data structure into a set like an example below.

Adding List in a Set

In the Set data structure lists and dictionaries can’t be added to a set, because they are mutable data structures. But adding a tuple is okay. So you cannot use the syntax as given in the code below, it will give an error.

But you can easily add tuples into the set:

Creating Empty Set

Creating an empty set is also quite simple, you can create an empty set by simply typing set().

Accessing to Set Items

I mentioned that the items in the set are not in order. Therefore, we cannot access the items using the index, but we can navigate through the set like the example below.

Finding Items in a Set

Since there is no index, it is impossible to find where an element is in the set. But we can find out if the item is in the set by writing an if-else condition.

Adding Items to a Set

There are two ways to add items into a set.

  • add()
  • update()

Add

With add method, you can add a new item to the set.

Update

If you want to add multiple items you should use the update method like the example below.

Deleting Items from a Set

Also there is various way to delete items from a set, these are:

  • remove()
  • discard()
  • pop()
  • clear()
  • del set

Remove

When you want to remove a certain item, you can use the remove method to do so. All you need to do is give the item as an argument.

Discard

The discard() method removes a certain item from the set. The difference between this method and the remove method is that the remove() method will raise an error if a certain item does not exist, and the discard() method will not.

Pop

The pop method is used for deleting a random item from the set.

Clear

When you want to clear your set you can use this method. In this way, you can use the set you created before as an empty set.

Del Set

So if you want to delete it you can use the del set, after deleting a set with this way it will be completely erased from memory and you will not be able to access this set again.

Operations That We Can Use on The Set

We can also do operations on python sets, such as the union, intersection, difference, which we do on the sets in mathematics.

  • union() -> |
  • intersection() -> &
  • difference() -> -
  • symmetric difference() -> ^

Union

The union() method returns a set that contains all items from the original set and all items from the specified set(s).

Intersection

The intersection() method returns a set that contains the similarity between two or more sets.

Intersection Update

The intersection update method returns a set that contains similar items between the sets given and updates the set.

Difference

The returned set contains items that exist only in the first set, and not in both sets.

Difference Update

The difference update() method updates the set with the items that only exist in the first set and not in the second.

Symmetric Difference

The symmetric difference method returns all the items present in given sets, except the items in their intersections.

Symmetric Difference Update

The symmetric difference update method finds the symmetric difference of two sets (non-similar items of both sets) and updates it to the set that calls the method.

Copying a Set

You can copy a set in many ways. These are:

  • copy()
  • = operator
  • set()

Copy

By using the copy method, you can copy one set to another.

= operator

If you want to use the equal (=) operator while copying, you should be careful a bit. Because with this way you're not creating a new set, you just define one more set that points to the other set.

Set

You can also use the set method to copy another set like the example below.

Subset and Superset

  • issubset() -> Is given set is a subset of the set
  • issuperset() -> Is given set is a superset of the set

Frozenset

Frozenset is used to create an immutable set.

And we’ve come to the end of another medium article! Today I tried to write about sets and their uses. I hope you enjoyed it. You can also check out my other articles about Python from here.

Also, don’t forget to follow me and my other social media accounts.

You can find all the resources I used in my article here!

Sources:

Geeks For Geeks Python Data Structures

Educative Python From Scratch

Programiz Python Set Methods

--

--

Elif Çelik
CodeX
Writer for

A Computer Engineer who is interested in Machine Learning and Deep Learning.