DEV Community

OLADEJOBI7
OLADEJOBI7

Posted on

Python Dictionary with examples

Dictionary is a mutable data type in Python. A python dictionary is a collection of key and value pairs separated by a colon (:), enclosed in curly braces {}.
Python Dictionary
Here we have a dictionary. Left side of the colon(:) is the key and right side of the : is the value.
mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'}
Points to Note:

  1. Keys must be unique in dictionary, duplicate values are allowed.
  2. A dictionary is said to be empty if it has no key value pairs. An empty dictionary is denoted like this: {}.
  3. The keys of dictionary must be of immutable data types such as String, numbers or tuples. Accessing dictionary values using keys in Python To access a value we can can use the corresponding key in the square brackets as shown in the following example. Dictionary name followed by square brackets and in the brackets we specify the key for which we want the value. mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'} print("Student Age is:", mydict['StuAge']) print("Student City is:", mydict['StuCity']) Output: Student Age is: 30 Student City is: Agra specify a key which doesn't exist in the dictionary then you will get a compilation error. For example. Here we are trying to access the value for key 'StuClass' which does not exist in the dictionary mydict, thus we get a compilation error when we run this code. mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'} print("Student Age is:", mydict['StuClass']) print("Student City is:", mydict['StuCity']) Output: Traceback (most recent call last): File "./prog.py", line 2, in KeyError: 'StuClass' Change values in Dictionary Here we are updating the values for the existing key-value pairs. To update a value in dictionary we are using the corresponding key. mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'} print("Student Age before update is:", mydict['StuAge']) print("Student City before update is:", mydict['StuCity']) mydict['StuAge'] = 31 mydict['StuCity'] = 'Noida' print("Student Age after update is:", mydict['StuAge']) print("Student City after update is:", mydict['StuCity']) Output: Student Age before update is: 30 Student City before update is: Agra Student Age after update is: 31 Student City after update is: Noida Adding a new entry (key-value pair) in dictionary We can also add a new key-value pair in an existing dictionary. Lets take an example to understand this. mydict = {'StuName': 'Steve', 'StuAge': 4, 'StuCity': 'Agra'} mydict['StuClass'] = 'Jr.KG' print("Student Name is:", mydict['StuName']) print("Student Class is:", mydict['StuClass']) Output: Student Name is: Steve Student Class is: Jr.KG Loop through a dictionary We can loop through a dictionary as shown in the following example. Here we are using for loop. mydict = {'StuName': 'Steve', 'StuAge': 4, 'StuCity': 'Agra'} for e in mydict: print("Key:",e,"Value:",mydict[e]) Output: Key: StuName Value: Steve Key: StuAge Value: 4 Key: StuCity Value: Agra Python delete operation on dictionary We can delete key-value pairs as well as entire dictionary in python. Lets take an example. As you can see we can use del following by dictionary name and in square brackets we can specify the key to delete the specified key value pair from dictionary. To delete all the entries (all key-value pairs) from dictionary we can use the clear() method. To delete entire dictionary along with all the data use del keyword followed by dictionary name as shown in the following example. mydict = {'StuName': 'Steve', 'StuAge': 4, 'StuCity': 'Agra'} del mydict['StuCity']; # remove entry with key 'StuCity' mydict.clear(); # remove all key-value pairs from mydict del mydict ; # delete entire dictionary mydict

Top comments (0)