In this post, learn every thing about Python Dictionaries.

What Is Python Dictionary

Python dictionary is a data structure which can store key, value pairs.

Note: The syntax of Python Dictionary is pretty simple. Every key value is separated by comma.

{'key1':'value1','key2':'value2'}

Python Create Dictionary

Example:

my_dict = {'Name': 'John', 'Job': 'Engineer'}

Python dictionaries can contain any kind of data. The keys and values could be string, characters, integers etc.

Example:

my_dict = {'Name': 'John', 'Job': 'Engineer','id':1}
print(my_dict)
{'Name': 'John', 'Job': 'Engineer', 'id': 1}

Python Empty Dictionary

Instead of initializing the dictionary with data, we can create an empty dictionary and then fill in the key values as shown below.

my_dict = {}
my_dict['Name'] = 'John'

Python print dictionary

print(my_dict)

Output of above command...

{'Name': 'John'}

Python Access Dictionary

We can access dictionary values in two ways.

  1. Python dictionary get key: Using the 'key' inside the square brackets directly dict['key']
my_dict = {'Name': 'John', 'Job': 'Engineer','id':1}
print(my_dict['Name'])

John

This method will throw an error if key doesn't exist as shown below...

print(my_dict['Networth'])

You will see following error...

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-110-8dd0654fca4b> in <module>
----> 1 print(my_dict['Networth'])

KeyError: 'Networth'

2. Python dictionary get key: Using the dict.get('key') method

get('key') method will return 'None' if key is not found.

my_dict = {'Name': 'John', 'Job': 'Engineer','id':1}
print(my_dict.get('Networth'))
None

Python Dictionary Keys

keys() method in Python Dictionary returns a view object that displays a list of all the keys in the dictionary.

Example: Let us print the keys of our Python dictionary.

keys = my_dict.keys()
print(keys)
dict_keys(['Name', 'Job', 'id', 'Place'])

Python Dictionary Items

dictionary.items() method returns a view object that displays a list of a given dictionary's (key, value) tuple pair as shown below...

my_dict = {'Name': 'John', 'Job': 'Engineer','id':1}
print(my_dict.items())
dict_items([('Name', 'John'), ('Job', 'Engineer'), ('id', 1)])

Python Add To Dictionary

Python dictionaries are mutable, we can do following...

  1. We can add new key, value pair in Python dictionary
  2. We can update value of key or append values to existing key in Python dictionary

1. Add new key, value pair in Python dictionary

my_dict = {'Name': 'John', 'Job': 'Engineer','id':1}
# Add new key, value : 'Place','California'
my_dict['Place'] = 'California'
print(my_dict)
{'Name': 'John', 'Job': 'Engineer', 'id': 1, 'Place': 'California'}

2. Update value of key in Python dictionary

my_dict = {'Name': 'John', 'Job': 'Engineer','id':1}
# Update key 'Name'
my_dict['Name'] = 'Alex'
print(my_dict)
{'Name': 'Alex', 'Job': 'Engineer', 'id': 1, 'Place': 'California'}

Python Delete From Dictionary

1. Python delete key from dict using pop() method

Example: Let us remove the key 'Name' from our dictionary.

my_dict = {'Name': 'John', 'Job': 'Engineer','id':1}
my_dict.pop('Name')
print(my_dict)
{'Job': 'Engineer', 'id': 1}

2. Delete Python Dictionary

Example: Remove the dictionary my_dict using 'del' method.

my_dict = {'Name': 'John', 'Job': 'Engineer','id':1}
del my_dict
print(my_dict)

Since the dictionary has been deleted, We will get following error when we print(my_dict).

NameError: name 'my_dict' is not defined

3. Python remove or delete everything from dictionary

We can either initialize the dictionary to empty Python dictionary or we can use dictionary.clear() method as shown below.

my_dict = {'Name': 'John', 'Job': 'Engineer','id':1}
#my_dict = {} or
my_dict.clear()
print(my_dict)
{}