Nearby lessons

32 of 108

Python - Dictionary Data Type

Introduction

If we want to store data as key-value pairs, then we should use the dict data type.

Each key in a dictionary is unique and is used to access its corresponding value.

Features of Dictionary

  1. Data is stored as key-value pairs.
  2. Duplicate keys are not allowed.
  3. Duplicate values are allowed.
  4. Heterogeneous values are allowed.
  5. Dictionary is mutable.
  6. Dictionary is growable.
  7. Insertion order is preserved.

Creating a Dictionary

Dictionary elements are enclosed within curly braces { }.

Syntax:

d = { key : value }

Example - Creating a Dictionary

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{100: 'durga', 200: 'ravi', 300: 'shiva'}

Explanation

Key Value
100 durga
200 ravi
300 shiva

Duplicate Keys are Not Allowed

If the same key appears more than once, the latest value replaces the previous value.

Example - Duplicate Keys

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{100: 'sunny', 200: 'ravi'}

Explanation

The key 100 appears twice.

The latest value "sunny" replaces the old value "durga".

Duplicate Values are Allowed

Dictionary values can be duplicated.

Example - Duplicate Values

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{1: 'python', 2: 'java', 3: 'python'}

Accessing Dictionary Values

We can access a value by using its key.

Example - Accessing Values

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
durga

Invalid Key Access

If the specified key does not exist, Python raises a KeyError.

Example - KeyError

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
KeyError

Modifying Dictionary Values

Since a dictionary is mutable, values can be modified after creation.

Example - Modifying Values

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{100: 'sunny', 200: 'ravi'}

Adding a New Key-Value Pair

We can add a new key-value pair by assigning a value to a new key.

Example - Adding New Data

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{100: 'durga', 200: 'ravi'}

Removing Elements Using del

The del statement removes a key-value pair from a dictionary.

Example - del Statement

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{200: 'ravi'}

Iterating a Dictionary

By default, a for loop iterates through dictionary keys.

Example - Iterating Keys

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
100
200

Accessing Keys and Values Together

Use the items() method to access both keys and values.

Example - items()

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
100 : durga
200 : ravi

Dictionary Methods

Some commonly used dictionary methods are:

  • keys() - Returns all keys.
  • values() - Returns all values.
  • items() - Returns all key-value pairs.

Example - keys()

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
dict_keys([100, 200])

Example - values()

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
dict_values(['durga', 'ravi'])

Example - items()

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
dict_items([(100, 'durga'), (200, 'ravi')])

Creating an Empty Dictionary

Use empty curly braces {} to create an empty dictionary.

Example - Empty Dictionary

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Nested Dictionary

A dictionary can contain another dictionary as a value.

Example - Nested Dictionary

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{'name': 'durga', 'marks': {'math': 90, 'science': 95}}

Real World Usage of Dictionary

Dictionaries are useful when data needs to be stored as key-value pairs.

Common applications include:

  • Student records
  • Employee details
  • Product information
  • API responses

Example - Employee Record

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{'id': 101, 'name': 'Rahul', 'salary': 50000}

Difference Between List and Dictionary

List Dictionary
Uses indexes. Uses keys.
Ordered collection. Key-value collection.
Uses [ ]. Uses { }.

Important Notes

  • Dictionary is mutable.
  • Duplicate keys are not allowed.
  • Duplicate values are allowed.
  • Insertion order is preserved.
  • Every key must be unique.

Key Points

  • A dictionary stores data as key-value pairs.
  • Values are accessed using keys.
  • Supports adding, modifying, and deleting data.
  • Supports iteration and built-in methods like keys(), values(), and items().
  • Useful for storing structured data.

Quick Summary

Feature Description
Data Typedict
MutableYes
Duplicate KeysNot Allowed
Duplicate ValuesAllowed
Insertion OrderPreserved
GrowableYes
Symbol{ }

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Base Conversion