smart and simple

This is going to be a very interesting article related to the set in python.

Properties of set in python:

  • Unique elements
  • Fast in search
  • Unordered elements


we can create set by placing elements inside curly braces( {} )

for example S1 = {10, 20, 30,40}

if we print S1, we will get output but that will not be in the same sequence as we have provided.

That’s why the set is an unordered collection of unique elements.

we can’t control indexing that’s why indexing is not possible in the set.

you can see we can’t fetch an element by using an index.

Let’s check the uniqueness.

yes, it stores only unique elements.

But the real question is how?

What is happening behind this?

how actually python developers managed uniqueness?

Let me clear to you what happens exactly…

They have used hashing concept over here.

Basically hashing is the technique of converting a given element into another value. A hash function is used to generate the new value according to a mathematical algorithm. The result of a hash function is known as a hash value.

here they have used strong hash algorithms like SHA, MD5, etc

remember for every similar element hash value will be the same.

hash value always unique for an element and one element cannot hold the same position two times.

This is how it stores elements and that is why the set is the unique collection of elements.


  • Set is a very powerful data structure for searching purposes.

Searching complexity of set is O(1)

this is very fast other than all data structures.

This is because if we want to search an element 30 in S2, It will first calculate the hash value of 30 and then will directly fetch data from the hash table.

In this case, a hash value for 30 is 2, so it will directly return Hash_value[2].

  • Set is mainly used for uniqueness and searching.

this is how the set data structure works.

In the next article, we will discuss some special functions in the set.

Thanks for reading !!!

Smart and Simple Solutions.