Home » Python Tutorial » Python Dictionary

Python Dictionary


A Python dictionary is a collection that is unordered, mutable, and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values.

d={'name':'Jason','age':12,'Gender':'M'}

In the above example, the keys are name, age, and gender.  And their respective values are Jason, 12 and M.

The data type of a dictionary value can be a list, tuple, string, etc. If the data type of a dictionary value itself a dictionary, then its called a nested dictionary.

Accessing elements from a Python Dictionary

Using the key

We can access the elements of the dictionary by its key, inside square brackets.

d={'name':'abc','age':12,'Gender':'M'}
print(d['name'])
print(d['age'])

A nested dictionary can also be accessed in the same way.

d={'fruits':{'a':'apple','b':'banana'}}
print(d['fruits'])
print(d['fruits']['a'])

get() method

We can also use the get() method to access the elements using the key.

d={'name':'abc','age':12,'Gender':'M'}
print(d.get('Gender'))

#Nested dictionary
d={'fruits':{'a':'apple','b':'banana'}}
print(d.get('fruits').get('a'))

Iterate through Python Dictionary

for loop

We can use for loop in a Python dictionary by 2 different methods (ways):

  • Accessing only the key in a for loop.
  • Accessing both keys and values in a for loop using items() method.  items() method returns the lists of all dictionary keys and their values in a tuple and this method doesn’t accept any parameters.
d={'name':'abc','age':12,'Gender':'M'}

# Method 1 - accessing key in for loop
for key in d:
  print(key)

# Method 2 - accessing key and value in for loop
for key,value in d.items():
  print(key+':'+str(value))

Changing elements in Python Dictionary

Add elements in Python Dictionary

We can add a new element to the dictionary by adding a new key and assigning a value to it.

Change the value

By assigning the required value to an existing key, the previous value of the key can be changed.

Remove elements from Dictionary

pop() in Dictionary

The pop() method removes the item with the specified key name.

popitem() in Dictionary

The popitem() method removes the last inserted element in the dictionary

del in Dictionary

The del keyword removes the item with the specified key name. If the key is not specified, the entire dictionary will be deleted.

clear in Dictionary

The clear() method clears the dictionary.

Code Examples Python Dictionary

d={'name':'abc','age':12,'Gender':'M'}

# pop
d.pop('age')
print(d)

# popitem
d.popitem()
print(d)

# add
d['age']=12
print(d)

# change
d['age']=13
print(d)

# del
del d['age']
del d

# clear
d={'name':'abc','age':12,'Gender':'M'}
d.clear()
print(d)

Other commonly used operations

len() in Dictionary

len() method is used to find the length of a dictionary.

in operator in Dictionary

To check if a specific key exists in a dictionary.

dict() constructor in Dictionary

The dict() constructor to make a new dictionary

Code Examples Python Dictionary

# dict()
d=dict(name='abc',age=12,Gender='M')
print(d)

# len
print(len(d))

# in
print("name" in d)
print("Name" in d)

Reference Python Tutorial