Dictionaries

A dictionary is a collection of key : value pairs

Dictionary is one of the most important data structures in Python programming.
It is used to store data in the form of:

Dictionaries are very useful for storing structured information such as:

  • student details
  • employee records
  • product information
  • banking data
  • login credentials

Python dictionaries help programmers organize data in a fast, efficient, and meaningful way.

What is a Dictionary in Python

A dictionary is a collection that is ordered (in newer Python versions) and changeable.

  • Key-Value Pairs: Every piece of data has a "Key" (the label) and a "Value" (the information).

  • Unique Keys: Every key must be unique—you can't have two labels with the same name!

  • Curly Braces: We create dictionaries using { } and colons : to connect keys to values.

  • Fast Lookup: You can find a value instantly just by knowing its key. It’s like searching for a friend’s name in your phone contacts!

Why Dictionaries are Important

Dictionaries are important because they help programmers:

  • organize data efficiently
  • access values quickly
  • store structured information
  • manage large datasets
  • improve program readability

Difference Between List, Tuple, Set, and Dictionary

Data Type Brackets Ordered Mutable
List [ ] Yes Yes
Tuple ( ) Yes No
Set { } No Yes
Dictionary { } Yes Yes

                                  


Real-World Dictionary Scenarios

  • Phone Book: (Name → Phone Number)

  • Fruit Stall: ("Apple" → $2, "Banana" → $1)

  • Student Card: ("Name" → "Ram", "Grade" → 5, "Roll No" → 12)

  • Game Inventory: ("Potions" → 5, "Swords" → 2)What is a Dictionary?

Syntax 

dictionary_name = {
    key1 : value1,
    key2 : value2,
    key3 : value3
}


Example 1: Finding Information

student = {"name": "Ram", "marks": 90}
print(student["name"])

Output: Ram


Example 2: Adding New Information

data = {"a": 1, "b": 2}
data["c"] = 3
print(data)


Output: {'a': 1, 'b': 2, 'c': 3}

Empty Dictionary in Python

An empty dictionary is created using:

{}

Example

d = {}

print(d)

print(type(d))

Output

{}

<class 'dict'>

Important Points About Dictionaries

  • dictionaries use curly brackets { }
  • data is stored using key : value
  • key-value pairs are separated using commas
  • keys must be unique
  • dictionaries are mutable

Real-Time Applications of Dictionaries

Application Usage
Student Management Systems Student details
Banking Applications Account information
E-Commerce Websites Product details
Login Systems Username and password
Social Media Apps User profile data

Common Beginner Mistakes 

Mistake Problem
Forgetting quotes around keys NameError
Accessing invalid key KeyError
Confusing set and dictionary Incorrect data type
Using duplicate keys Old value overwritten

Advantages of Dictionaries

  • fast data access
  • organized structure
  • easy updates
  • supports dynamic data
  • efficient key-based searching

Summary:

  • Mutable: You can add, change, or delete key-value pairs anytime.

  • No Duplicates for Keys: If you try to use the same key twice, the new value will overwrite the old one.

  • Structured Data: It’s the best tool for keeping related information together in one neat package.

Dictionaries are widely used in:

  • banking systems
  • websites
  • student management systems
  • e-commerce applications
  • real-world software development

Check your knowledge

Quickly verify what you've learned from this tutorial.

Question 1

Which characters are used to define a Dictionary in Python?

Python uses curly braces { } to store key-value pairs in a dictionary.

Question 2

In a dictionary, what must be unique?

Keys act like unique IDs; you cannot have two identical keys in one dictionary.

Question 3

How do you access the value '90' from this dictionary: results = {"math": 90, "science": 85}?

You access data by placing the key inside square brackets.

Question 4

What happens if you run my_dict["age"] = 15 on a dictionary that doesn't have an "age" key yet?

Dictionaries are mutable, so assigning a value to a new key simply adds it to the collection.

Question 5

Why are Dictionaries better than Lists for a "Phone Book" app?

Dictionaries provide fast data lookup, making them much more efficient for searching specific labels than a list.

Congratulations!

You've successfully mastered the knowledge check for "Dictionaries."

For more questions and practice, click the link below:

Practice More Questions
Previous Topic Sets Next Topic Dictionary Operations