DEV Community

VIDHYA VARSHINI
VIDHYA VARSHINI

Posted on

Day 6 of Python (Dictionary, Set and Hashing)

What is dictionary: It is a collection used to store data in key:value pairs and it is a built-in data type, mutable and ordered.

Ex:

id = { name = "Vidya",
       age : 21
       location : "Chennai"
       gender : "Female"
     }
print(id)
Enter fullscreen mode Exit fullscreen mode

Output:

{'name': 'Vidya', 'age': 21, 'location': 'Chennai', 'gender': 'Female'}
Enter fullscreen mode Exit fullscreen mode

Set: It is a collection of items which is unordered, does not allow duplicate values and mutable.

Ex:

num = [1,1,2,2,2,3,3,5,5,5]
freq = {}

for i in num:
    if i in freq:
        freq[i] = freq[i] + 1
    else:
            freq[i] = 1
            print(freq)
Enter fullscreen mode Exit fullscreen mode

Output:

{1: 2, 2: 3, 3: 2, 5: 1}

Enter fullscreen mode Exit fullscreen mode

Hashing: It is a technique that takes a key and converts it into a fixed integer value (hash value). This hash value is used to decide where to store and quickly retrieve data in memory.
Ex:

print(hash("Vidya"))
Enter fullscreen mode Exit fullscreen mode

Output:

45823984723984
Enter fullscreen mode Exit fullscreen mode

Hash collision: Sometimes two different keys produce same hash, which causes collision, open addressing and probing.

Top comments (0)