How are dictionaries implemented in Python?

Dictionary in python is just like a map in C++ and Java. The Iike Map dictionary consists of two things: first is key and second is value. Dictionary is dynamic in nature. You can add more keys and values to the dictionary after creating the dictionary and also you can delete the key and value in the dictionary. You can add another dictionary to the currently created dictionary. Also you can add the list to the dictionary and dictionary to list.

In the dictionary you can access the elements via their respective keys.

Dictionary = { 1: "Apple", 2: "Ball", 3: "Caterpillar", 4: "Doctor", 5: "Elephant" }

Here in dictionaries 1, 2, 3 … are representing the keys and “Apple”, “Ball”, “Caterpillar”....represent the value.

Dictionary = { Key: "Value", Key : "Value", . . . . . Key : "Value" }

Access the elements

print(dictionary[1]) #print element whose key value is 1 i.e. “Apple” print(dictionary[4]) # print element whose key value is 4 “Doctor”

Insert and update elements in Dictionary

Dictionary[6] = "Flesh" # inserting element with key value 6 at last in dictionary Dictionary[3] = "Cat" # element with key value 3 is update with value “Cat”

Delete element in Dictionary

Dictionary.pop(3) # Delete element with key value 3 del Dictionary[4] # delete element with key value 4 Dictionary.popitem() # delete last inserted element in dictionary del Dictionary # this will delete whole dictionary

Built - in function for dictionary in python

  • Dictionary_2 = Dictionary.copy()

    This copy function will copy the all values of dictionary to Dictionary_2

  • Dictionary.clear()

    clear() function will clear the whole dictionary.

  • Dictionary.get(2)

    get() function will return the value of key 2.

  • Dictionary.values()

    This function will return all the values of the Dictionary.

  • Dictionary.update({5:”Ears”})

    This function will update the value of given key