Nearby lessons
32 of 108Python - 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
- Data is stored as key-value pairs.
- Duplicate keys are not allowed.
- Duplicate values are allowed.
- Heterogeneous values are allowed.
- Dictionary is mutable.
- Dictionary is growable.
- Insertion order is preserved.
Creating a Dictionary
Dictionary elements are enclosed within curly braces { }.
Syntax:
d = { key : value }
Example - Creating a Dictionary
main.py
{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
{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
{1: 'python', 2: 'java', 3: 'python'}Accessing Dictionary Values
We can access a value by using its key.
Example - Accessing Values
main.py
durga
Invalid Key Access
If the specified key does not exist, Python raises a KeyError.
Example - KeyError
main.py
KeyError
Modifying Dictionary Values
Since a dictionary is mutable, values can be modified after creation.
Example - Modifying Values
main.py
{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
{100: 'durga', 200: 'ravi'}Removing Elements Using del
The del statement removes a key-value pair from a dictionary.
Example - del Statement
main.py
{200: 'ravi'}Iterating a Dictionary
By default, a for loop iterates through dictionary keys.
Example - Iterating Keys
main.py
100 200
Accessing Keys and Values Together
Use the items() method to access both keys and values.
Example - items()
main.py
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
dict_keys([100, 200])
Example - values()
main.py
dict_values(['durga', 'ravi'])
Example - items()
main.py
dict_items([(100, 'durga'), (200, 'ravi')])
Creating an Empty Dictionary
Use empty curly braces {} to create an empty dictionary.
Example - Empty Dictionary
main.py
No output captured.
Nested Dictionary
A dictionary can contain another dictionary as a value.
Example - Nested Dictionary
main.py
{'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
{'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(), anditems(). - Useful for storing structured data.
Quick Summary
| Feature | Description |
|---|---|
| Data Type | dict |
| Mutable | Yes |
| Duplicate Keys | Not Allowed |
| Duplicate Values | Allowed |
| Insertion Order | Preserved |
| Growable | Yes |
| Symbol | { } |