Course Content
🎁 Bonus Modules (Integrated Throughout)
Data Analytics
Dictionaries 

What Is a Dictionary?

A dictionary is a built-in Python data type used to store key-value pairs. Unlike lists or tuples (which store values by position/index), dictionaries allow you to store data in a way where each value is linked to a unique key. This makes lookup, insertion, and deletion of values fast and efficient.

Think of it like a real-life dictionary: you look up a word (key) and find its definition (value).


Syntax & Structure

The basic structure of a Python dictionary is:

my_dict = {
"name": "Alice",
"age": 25,
"city": "Mumbai"
}
  • Keys must be unique and immutable (strings, numbers, or tuples).
  • Values can be of any data type, including other dictionaries or lists.

Accessing & Modifying Values

 

To get a value:

print(my_dict["name"]) # Output: Alice

To modify a value:

my_dict["age"] = 26

To add a new key-value pair:

my_dict["gender"] = "Female"

To remove a key:

del my_dict["city"]

To safely access a key without an error:

print(my_dict.get("country", "Not Found")) # Output: Not Found
Useful Dictionary Methods
Method Description
.get(key) Returns the value for the key if it exists, else returns None or default value.
.keys() Returns a view of all keys in the dictionary.
.values() Returns a view of all values.
.items() Returns key-value pairs as tuples.
.update() Adds or updates key-value pairs.
.pop(key) Removes the item with the specified key and returns its value.
0% Complete
WhatsApp Icon

Hi Instagram Fam!
Get a FREE Cheat Sheet on System Design.

Hi LinkedIn Fam!
Get a FREE Cheat Sheet on System Design

Loved Our YouTube Videos? Get a FREE Cheat Sheet on System Design.