BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Dictionary with examples

Last Updated: March 29, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

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:
Python dictionary example

If you 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:
Python dictionary error

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:
Python dictionary update values

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:
Python adding new key value pair in dictionary

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:
Python loop through dictionary

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
❮ PreviousNext ❯

Top Related Articles:

  1. Python Keywords and Identifiers with examples
  2. Python Recursion
  3. Python Constructors – default and parameterized
  4. Python Set union() method with examples
  5. Python all() Function with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Joel Z. Dolakeh says

    August 19, 2020 at 9:39 AM

    Thanks for making me better understand python dictionary

    Reply
  2. Maaz says

    November 21, 2020 at 11:56 AM

    You helped me alot with datatypes as an absolute beginner in Python. Thank you soooo much.

    Reply

Leave a Reply Cancel reply

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

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap