Member-only story
Day 7 of #100DaysOfCode in Python: Diving Into Dictionaries
Welcome to Day 7 of your #100DaysOfCode journey in Python! A full week of coding — congratulations on this significant milestone. Today, we’re going to delve deep into dictionaries, a powerful data structure in Python that is invaluable for mapping key-value pairs. By the end of this article, you’ll understand how to harness the capabilities of dictionaries in Python effectively.
What are Dictionaries?
In Python, a dictionary is an unordered collection of data stored in a key-value pair format. It provides a way to map unique keys to values, which can be of any data type.
Here’s a simple example:
# A basic dictionary
person = {
"name": "Alice",
"age": 30,
"city": "Wonderland"
}
Why Use Dictionaries?
- Efficient Mapping: Dictionaries allow for quick access to data based on unique keys.
- Dynamic: Like lists, dictionaries are mutable, meaning you can modify their data.
- Versatile: The values in dictionaries can be of any type and can even include other dictionaries or lists.
Creating Dictionaries
Dictionaries are created by placing key-value pairs inside curly braces {}
separated by commas.
# An empty dictionary
my_dict = {}
# A dictionary with integer keys
my_dict = {1: 'apple', 2: 'banana'}
# A dictionary with…