TGViewer
Python Learning Python Learning @python_bds Β· 5.74K subscribers
Post #1168 995
🐍 Python Dictionaries (Key-Value Pairs) πŸ”‘

Dictionaries are unordered collections of data that store items in key-value pairs. They are incredibly powerful for organizing and quickly accessing related information.

πŸ‘‰ Ideal for representing real-world objects with unique identifiers (keys).

πŸ”Ή 1. What is a Dictionary?

A mutable, unordered collection where each item has a unique key and an associated value. Defined by curly braces {}.

Example:
person = {"name": "Alice", "age": 30, "city": "New York"}
print(person)

Output: {'name': 'Alice', 'age': 30, 'city': 'New York'}

πŸ”Ή 2. Accessing Dictionary Items

Access values using their associated key inside square brackets [] or with the .get() method.

Example:
student = {"id": 101, "name": "Bob", "grade": "A"}
print(student["name"])
print(student.get("grade"))

Output:
Bob
A

πŸ”Ή 3. Adding and Modifying Items

To add a new item, assign a value to a new key. To modify, assign a new value to an existing key.

Example:
car = {"brand": "Ford", "model": "Mustang"}
car["year"] = 2020 # Add new item
car["model"] = "Explorer" # Modify existing item
print(car)

Output: {'brand': 'Ford', 'model': 'Explorer', 'year': 2020}

πŸ”Ή 4. Removing Items

β€’ del dict[key]: Removes the item with the specified key.
β€’ .pop(key): Removes (and returns) the item with the specified key.
β€’ .clear(): Empties the entire dictionary.

Example:
settings = {"theme": "dark", "sound": "on", "notifications": "off"}
del settings["sound"] # Remove by key
print(settings)
settings.pop("theme") # Also removes by key
print(settings)

Output:
{'theme': 'dark', 'notifications': 'off'}
{'notifications': 'off'}

πŸ”Ή 5. Looping Through a Dictionary

You can loop through keys, values, or both (items).

Example:
for k, v in person.items(): # Loops through key-value pairs
print(f"{k}: {v}")

Output:
name: Alice
age: 30
city: New York

🎯 Today's Goal(What you should do)

βœ”οΈ Understand how dictionaries store data in key-value pairs
βœ”οΈ Add, access, modify, and remove dictionary items
βœ”οΈ Iterate over keys, values, or items
  • ❀ 5
More from @python_bds
  1. Oct 8, 2026πŸ› This Python Bug Looks Random, But It Isn't Look at this: functions = [] for i in range(…
  2. Oct 6, 2026🐍 Python Performance Optimization Python Performance Optimization: Make Your Code Faster…
  3. Oct 4, 2026Python Set Methods ✍️
  4. Oct 3, 2026Post #1310
  5. Oct 1, 2026🐍 Python Beginner Notes
  6. Sep 30, 2026🧠 return vs print() in Python These are not interchangeable. def add(a, b): print(a + b)…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook β†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 β†’