What are Python dictionaries?
A dictionary in Python can be compared to a real-life dictionary: They contain a key, like a word. Each key has a corresponding value, like a definition. Just like how lists are made with square brackets, dictionaries are made with squiggly brackets.
For example:
students = {"James": 14, "Charlie": 16, "Ezgi": 18, "Veronnica":19, "Abdullah":20}
# The names are the keys, and the ages are the values
In order to access a value from a key, we would do a similar operation as we did with lists and place the key into square brackets. For example, let's receive Abdullah's age as shown below:
abdullah_age = students["Abdullah"]
print(f"Abdullah's age is {abdullah_age}")
Output:
Abdullah's age is 20
In order to change a key's value, we would do a procedure similar to accessing a key's value by writing the key in square brackets but setting the value to our new value using an equals to sign. For example, let's change Ezgi's age:
students["Ezgi"] = 19 # changing the values
print(f"Ezgi's age is {students['Ezgi']}")
Output:
Ezgi's age is 19
In order to remove a key and it's value from a dictionary, we would use the del keyword we learned about previously. For example, let's remove Charlie and her value:
del students["Charlie"] # removing an element of the dictionary
print(students)
Output:
students = {"James": 14, "Ezgi": 19, "Veronnica":19, "Abdullah":20}
In order to remove a key and it's value from a dictionary, we would use the del keyword we learned about previously. For example, let's remove Charlie and her value:
del students["Charlie"] # removing an element of the dictionary
print(students)
Output:
students = {"James": 14, "Ezgi": 19, "Veronnica":19, "Abdullah":20}
Considering dictionaries and lists are similar in the sense that they carry multiple pieces of data, we can transfer dictionaries into lists. We can do these for either the keys of the dictionaries or the values as shown below:
student_lists = list(students.keys())
print(student_lists) #gets a list of student keys
student_ages = list(students.values())
print(student_ages) #gets a list of student values
Output:
['James', 'Ezgi', 'Veronnica', 'Abdullah']
[14, 19, 19, 20]
We can add a brand new element to the dictionary as shown below:
students["Ceylin"] = 21 # adding a new student
print(students)
Output:
{'James': 14, 'Ezgi': 19, 'Veronnica': 19, 'Abdullah': 20, 'Ceylin': 21}
One important aspects of dictionaries is the fact that they can be combined with lists and even other dictionaries! Below is an example:
countries = {"Canada": ["Ottawa", 1897],
"Netherlands": ['Amsterdam', 1814],
"Belgium": ["Brussels", 1830]}
print(f"The capital of Canada is {countries['Canada'][0]}")
print(f"The independence year of Belgium is {countries['Belgium'][1]}")
print(f"The capital and independence year of the Netherlands is {countries['Netherlands'][0], countries['Netherlands'][1]}")
countries2 = {"Canada": {"capital": "Ottawa", "independence_year": 1897},
"Netherlands": {"capital": "Amsterdam", "independence_year": 1814}}
print(f"The capital of Canada is {countries2['Canada']['capital']}")
print(f"The independence year of Canada is {countries2['Canada']['independence_year']}")
Make a dictionary containing the 5 cities you want to visit most. As for the values of these cities, set a list for each of them which will include, in order,
1 - What country that city is in
2 - Your favourite dish from that city/country
3 - The top thing you would like to see within that city.
Step 1: Create a dictionary to contain your cities.
Step 2: Write a city name as a key.
Step 3: Make a list as the value of that key.
Step 4: Within the list write, in order, the country of the city, the food you'd like to try and something you would like to see in that city.
Step 5: Repeat for 4 other cities.
This is only a suggested answer
cities = {
"Paris": ["France", "Croissant", "Eiffel Tower"],
"Tokyo": ["Japan", "Sushi", "Imperial Palace"],
"New York": ["USA", "Burgers", "Central Park"],
"Istanbul": ["Turkey", "Kebab", "Blue Mosque"],
"Madrid": ["Spain", "Paella", "Gaudi Square"]
}
You have reached the end of Section 6: Dictionaries!