DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 54 of 77
←PreviousPrevNextNext→

Python Dictionaries

What is a Dictionary?

A dictionary is a built-in data structure in Python that stores data as key-value pairs. It is mutable, allows fast lookups by key, and can hold heterogeneous data types.

Dictionaries are defined with curly braces `{}` containing key:value pairs, or by using the `dict()` constructor.

Example
# Creating dictionaries
person = {"name": "John", "age": 30, "city": "New York"}
empty_dict = {}

# Using dict() constructor
person2 = dict(name="Jane", age=25, city="London")

print(person)
print(person2)
Output
{'name': 'John', 'age': 30, 'city': 'New York'}
{'name': 'Jane', 'age': 25, 'city': 'London'}

Dictionary Characteristics

  • Insertion order is preserved (Python 3.7+).
  • Mutable: You can add, remove, and update key-value pairs.
  • Keys must be immutable (e.g., strings, numbers, or tuples with immutable elements).
  • Values can be any type (strings, numbers, lists, other dictionaries, etc.).
  • Keys must be unique; assigning an existing key overwrites its value.

Dictionary Use Cases

Dictionaries are useful for:

- Storing related information such as object properties

- Counting occurrences of items (e.g., word frequencies)

- Mapping relationships between values (e.g., IDs to names)

- Caching or memoizing function results

- Managing configuration and settings

Test your knowledge: Python Dictionaries
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 54 of 77
←PreviousPrevNextNext→