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 = { "key1": "value1", "key2": "value2", "key3": "value3", }
For example, you can define a dictionary to store a person's information as follows:
person = { "name": "John", "age": 30, "job": "Developer" }
In the code above, the person dictionary contains keys such as name, age, and job, with their corresponding values.
What are the Features of a Dictionary?
Dictionaries have the following features:
-
Fast Lookup: Dictionaries store data as key-value pairs, enabling fast retrieval of values using their keys. -
No Index-based Access: Dictionaries are unordered data structures, meaning you cannot access elements by index as you can with lists or tuples. -
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 :.
# Declaring an empty dictionary empty_dict = {} # Declaring a dictionary with key-value pairs product = { "name": "Orange", "price": 1000, "best_before": "2024-12-31" }
What is the most appropriate answer to fill in the blank below?
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result