Skip to main content
Practice

Managing Structured Data with Dictionaries

A dictionary is a data structure that efficiently manages and retrieves data by storing it as pairs of keys and values.

Dictionary Structure
dictionary = {
"key1": "value1",
"key2": "value2",
"key3": "value3",
}

For example, you can define a dictionary to store a person's information as follows:

Dictionary Storing Person's Information
person = {
"name": "John",
"age": 30,
"job": "Developer"
}

In the code above, the person dictionary contains keys like name, age, and job, with their corresponding values.


What are the Features of a Dictionary?

Dictionaries have the following features:

  1. Fast Lookup: Dictionaries store data as key-value pairs, enabling fast retrieval of values using their keys.

  2. No Index-based Access: Dictionaries are unordered data structures, meaning you cannot access elements by index as you can with lists or tuples.

  3. Unique Keys: Each key in a dictionary must be unique; duplicate keys are not allowed within the same dictionary.


How to Declare a Dictionary?

Dictionaries are defined using curly braces {}, with keys and values connected by a colon :.

Example of Dictionary Declaration
# Declaring an empty dictionary
empty_dict = {}

# Declaring a dictionary with key-value pairs
product = {
"name": "Orange",
"price": 1000,
"best_before": "2024-12-31"
}

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.