Dictionaries
Introduction to Dictionaries
A dictionary is a data structure in programming that stores a collection of key-value pairs. Each key in the dictionary is unique and associated with a specific value. It is similar to a physical dictionary where words (keys) have corresponding meanings (values).
How Dictionaries Work
In dictionaries, keys are used to access their corresponding values. This allows for efficient data retrieval as the unique keys provide a direct mapping to the desired values. Dictionaries are commonly used when we want to quickly look up values based on given keys.
Creating a Dictionary
In Python, dictionaries are represented using curly braces ({}) and key-value pairs separated by a colon (:).
Here's an example:
# Creating a dictionary
student = {"name": "John", "age": 16, "grade": "11th"}What is a dictionary in Python?
{"answers": {"A": "A data structure that stores a collection of elements, each identified by a key.", "B": "A programming language used for creating web applications.", "C": "None of the above.", "D": "A function that allows you to input data."}, "question_text": "What is a dictionary in Python?", "correct_answer": "A"}
Accessing Values in a Dictionary
Accessing Values in a Dictionary
To access the values in a dictionary, we can use the corresponding keys. Here's an example:
# Accessing values in a dictionary
print(student["name"]) # Outputs: John
print(student["age"]) # Outputs: 16
print(student["grade"]) # Outputs: 11th
Modifying Dictionary Values
Dictionaries allow us to modify the values associated with existing keys. We simply assign a new value to the corresponding key. Here's an example:
# Modifying dictionary values
student["grade"] = "12th"
print(student["grade"]) # Outputs: 12th
Telephone Numbers and Emails
Here is an example of how you can create a dictionary in Python to store telephone numbers and emails:
contacts = {
"John Doe": {
"telephone": "123-456-7890",
"email": "[email protected]"
},
"Jane Smith": {
"telephone": "987-654-3210",
"email": "[email protected]"
},
"Alex Johnson": {
"telephone": "555-123-4567",
"email": "[email protected]"
}
}How to Access the Data?
You can access the telephone numbers and emails stored in the dictionary by using the respective person's name as the key:
johns_telephone = contacts["John Doe"]["telephone"]
janes_email = contacts["Jane Smith"]["email"]
alexs_telephone = contacts["Alex Johnson"]["telephone"]
Now, `johns_telephone` will contain the value "123-456-7890", `janes_email` will contain "[email protected]", and `alexs_telephone` will contain "555-123-4567".
A key in a Python dictionary must be of a(n) [[immutable]] type.
{"cloze_text": "A key in a Python dictionary must be of a(n) [[immutable]] type."}
Dictionaries & Hast Tables
Python dictionaries are implemented using a computer science technique know as hash tables. A hash table is a data structure that provides fast data retrieval by mapping keys to values using a hash function, ensuring efficient storage and quick access.
The most significant advantage of using a hash table is the average O(1) time complexity for lookups, insertions, and deletions. This means that accessing or modifying elements is very fast, regardless of the size of the dictionary.
{"keyword": "Hash Table", "definition": "A data structure that allows for efficient access using a hash function."}
Python Dictionaries
- Introduction to Dictionaries
- Accessing Values in a Dictionary
- Telephone Numbers and Emails
- Dictionaries & Hast Tables
Topic Tests
Š learnlearn.uk 2025 |
[email protected] |
Privacy Policy.