python tutorial

Python Dictionary

Learn more about Python, one of the world’s most versatile and popular programming languages.

Python dictionaries are another powerful data type for representing information in Python. Dictionaries are an unordered collection of key-value pairs. There are many different ways of creating a Python dictionary however the easiest way is to use curly brackets {}. A dictionary is of the form

My_dict = {key: value, anotherKey: anotherValue}

Some of the important things to remember about dictionaries are:

  • Each key in the dictionary has to be unique
  • Unlike list, dictionaries are indexed by keys
  • A specific value in the dictionary can be retrieved by using it’s key.
  • Multiple key value pairs are separated by comma ,
  • Key and value are separated by a colon :

Creating a Python Dictionary

Let’s create a python dictionary using

  • Curly brackets
  • using dict()
Code Example
# create a dictionary using curly brackets called john that stores name, phone, email, course and address
john = {'name': 'John', 'phone': 1234567890, 'email': 'john.doe@gmail.com', 'course: Python v3', 'address': '123 Front St City Country'}
print(john)
{'name': 'John', 'phone': 1234567890, 'email': 'john.doe@gmail.com', 'course': 'Python v3',
'address': '123 Front St City Country'}
# create a dictionary using dict() called adam that stores name, phone, email, course
adam = dict ({'name': 'Adam', 'phone': 1234567890, 'email': 'adam.smith@email.com',
'course': 'Python v3'})
print(adam)
{'name': 'Adam', 'phone': 1234567890, 'email': 'adam.smith@email.com', 'course': 'Python v3'}
print(john)
{'name': 'John', 'phone': 1234567890, 'emall': 'john.doe@gmail.com', 'course': 'Python v3', 'address': '123 Front St City Country'}
print(adam)
{'name': 'Adam', 'phone': 1234567890, 'email': 'adam.smith@email.com', 'course': 'Python v3'}

Accessing a Value in Dictionary

Values in the dictionary can be accessed by using it’s relevant keys inside of a square bracket []. Let’s try to access the name and email of students adam and john we just created.

Code Example
# create a dictionary using curly brackets called john that stores name, phone, email, course and address
john = {'name': 'John', 'phone': 1234567890, 'email': 'john.doe@gmail.com', 'course': 'Python v3',
'address': '123 Front St City Country'}
print(john['name'])
print(john['email'])
John
john.doe@gmail.com
# create a dictionary using dict() called adam that stores name, phone, email, course
adam = dict({'name': 'Adam', 'phone': 1234567890, 'email': 'adam.smith@email.com', 'course': 'Python v3'})
print(adam['name'])
print(adam['email'])
Adam
adam.smith@email.com

Updating a Value in Dictionary

Values in the dictionary can be updated by using its key in a square bracket and assigning a new value to it using an equal sign =. Let’s update the phone number for both students adam and john

Code Example
# create a dictionary using curly brackets called john that stores name, phone, email, course and address
john = {'name': 'John', 'phone': 1234567890, 'email': 'john.doe@gmail.com', 'course': 'Python v3', 'address': '123 Front St City Country'}
print(john)
{'name': 'John','phone': 1234567890, 'email': 'john.doe@gmail.com', 'course': 'Python v3', 'address': '123 Front St City Country'}
# create a dictionary using dict() called adam that stores name, phone, email, course
adam = dict ({'name': 'Adam', 'phone': 1234567890, 'email': 'adam.smith@email.com', 'course': 'Python v3'})
print(adam)
{'name': 'Adam', 'phone': 1234567890, 'email': 'adam.smith@email.com', 'course': 'Python v3'}
# update phone numbers for john and adam
john['phone'] = 9876543210
adam['phone'] = 4567890123
print(john)
print(adam)
{'name': 'John', 'phone': 9876543210, 'email': 'john.doe@gmail.com', 'course': 'Python v3', 'address': '123 Front St City Country'}
{'name': 'Adam', 'phone': 4567890123, 'email': 'adam.smith@email.com', 'course': 'Python v3'}

Deleting a Value in Dictionary

Values in the dictionary can be deleted by using the pop method and specify the key of the value to be deleted as pop(key). Deleting a value also removes the entire key-value pair from the dictionary. Let’s try to delete the address for student john.

Code Example
# create a dictionary using curly brackets called john that stores name, phone, email, course and address
John = {'name': 'John', 'phone': 1234567890, 'email': 'john.doe@gmail.com', "course': 'Python v3', 'address': '123 Front St City Country'}
print(john)
{'name': 'John', 'phone': 1234567890, 'email': 'john.doe@gmail.com', 'course': 'Python v3', 'address': '123 Front St City Country'}
# create a dictionary using dict() called adam that stores name, phone, email, course
adam = dict ({'name': 'Adam', 'phone': 1234567890, 'email': 'adam.smith@email.com', 'course': 'Python v3'})
print(adam)
{'name': 'Adam', 'phone': 1234567890, 'email': 'adam.smith@email.com', 'course': 'Python v3'}
# delete address for john
john.pop('address')
print(john)
{'name': 'John', 'phone': 1234567890, 'email': 'john.doe@gmail.com', 'course': 'Python v3'}

Deleting Entire Dictionary

An entire dictionary can be deleted using the del keyword. This deletes the entire dictionary and not just the key-value pairs in it. Let’s delete our dictionary structure for student adam. As you can see below, accessing the dictionary after deleting it throws an error.

Code Example
# create a dictionary using curly brackets called john that stores name, phone, email, course and address
john = {'name': 'John', 'phone': 1234567890, 'email': 'john.doe@gmail.com', 'course': 'Python v3', 'address': '123 Front St City Country'}
print(john)
{'name': 'John', 'phone': 1234567890, 'email': 'john.doe@gmail.com', 'course': 'Python v3', 'address': '123 Front St City Country'}
# create a dictionary using dict() called adam that stores name, phone, email, course
adam = dict({'name': 'Adam', 'phone': 1234567890, 'email': 'adam.smith@email.com', 'course': 'Python v3'})
print(adam)
{'name': 'Adam', 'phone': 1234567890, 'email': 'adam.smith@email.com', 'course': 'Python v3'}
# delete dictionary structure adam
del adam
print(adam)
NameError	Traceback (most recent call last)
<ipython-input-35-a971909cf5b7> in <module>
    2 del adam
    3
----> 4 print(adam)
NameError: name 'adam' is not defined

Learn Python Today

Get hands-on experience writing code with interactive tutorials in our free online learning platform.

  • Free and fun
  • Designed for beginners
  • No downloads or setup required