What is Dictionary in Python and Methods of Dictionary in Python?

Dictionary is one of the four built in collection data type, used to store multiple or different types of data under single variable.

However, these data are not stored similar to lists, tuple or sets. The data here are stored in key value pairs and curly brackets is used to store its data. This is how a dictionary looks like.

Dictionary-in-python

As you can see, we can store either string, integer, Boolean or even list, but the point to be noted here is that we can also have our keys in integer as well as in string format.

Functionality or features of dictionary.

Dictionary items are ordered meaning the order in which we store items are preserved that way, unlike set.

  • Dictionary is mutable, meaning we can add or delete items from dictionary.
  • Dictionary does not allow duplicate keys, but it allows duplicate values. Also, if you insert duplicate key, it will not throw an error but the most recent key will be referred with its value and the previous duplicate key will get overwritten.

duplicatekeysinput

output

You Can Code From Here.

dictionary = {1:’Student’,
‘Name’:’Waqar Khan’,
‘Age’:23,
‘Male’:True,
‘Subjects’:[‘Physics’,’Maths’,’Computers’],
‘Subjects’:[‘Chemistry’,’Maths’,’Computers’]}

print(dictionary[‘Subjects’])

As you can see, we have duplicate keys. So, the most recent value is updated in those duplicate key and thus we get chemistry, maths and computers as output.

Properties:

  • Dictionary is collection data type
  • Items are ordered
  • Mutable
  • Insertion order is not preserved
  • Heterogenous (Different types of data can be stored)
  • No indexing no slicing

Dictionary constructor.

Dictionary has two types of constructors in which we can define empty dictionary and further use it according to needs. Either we can construct a dictionary with curly {} braces or using dict keyword with round brackets.

dict_constructor_in_python

dictonary_output_python

waqar = {}
print(type(waqar))

khan = dict()
print(type(khan))

As you can see, we have declared an empty dictionary with two different types of constructors.

Methods of dictionary.

Like sets, tuple and lists dictionaries too have their built-in methods and these built-in methods give us extra opportunity or say extra flexibility to work with it’s data. There are 11 built-in methods in dictionary and below table shows the same.

Methods Description
clear() Clears all the elements from the dictionary and returns the empty dictionary.
copy() Copies the entire dictionary.
fromkeys() Gives a dictionary with specific key and specific value
get() Returns the value of specific key
items() Returns a key value pair in view object
keys() Returns a key in view object
pop() Removes the specified item from the dictionary.
popitem() Removes the last inserted item from the dictionary.
setdefault() Returns the value of specified key, if key does not exist it simply puts the item.
update() Inserts the specified item in to dictionary.
values() Returns the values in view object.

Related Post:

Waqar Khan

Hello, everyone, I am Waqar Khan. I have done my B.tech in Computer Science. I love to write about Computer Informational blogs. Before write I did proper research on it. Hope you like our blogs. Thank you.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *