18. Dictionaries
18.1. Dictionary structure
key: value
pair.- dict_var = {key1 : value1, key2 : value2, …..}
Returns a dictionary with the specified key: value pairs.
eastern_state_capitals = {
'Victoria': 'Melbourne',
'New South Wales': 'Sydney',
'Queensland': 'Brisbane'
}
print(eastern_state_capitals)
18.2. Getting a value from a dictionary
- value_1 = dict_var[key1]
Returns a value in a dictionary with the specified key, key1.
eastern_state_capitals['Victoria']
.eastern_state_capitals = {
'Victoria': 'Melbourne',
'New South Wales': 'Sydney',
'Queensland': 'Brisbane'
}
capital = eastern_state_capitals['Victoria']
print(capital)
# Output is 'Melbourne'
18.3. Case sensitive keys
eastern_state_capitals = {
'Vic': 'Melbourne',
'VIC': 'MELB',
}
print(eastern_state_capitals['Vic'])
# Output is 'Melbourne'
print(eastern_state_capitals['VIC'])
# Output is 'MELB'
18.4. Empty dictionary
empty_dict = {}
empty_dict = dict()
18.5. Making a dictionary
18.5.1. Making a dictionary: curly brackets
{}
.state_capitals = {
'Victoria': "Melbourne",
'Tasmania': "Hobart",
'Queensland': "Brisbane"
}
print(state_capitals)
Tasks
Create a dictionary using curly brackets such that it maps the names of three countries, Japan, France and England, to their capitals: Tokyo, Paris and London. Print the dictionary.
Create a dictionary using curly brackets such that it maps the names of three fruits, Apple, Banana, and Grapes, to their colors: Red, Yellow, and Purple. Print the dictionary.
Create a dictionary using curly brackets such that it maps the names of three countries, Japan, France and England, to their capitals: Tokyo, Paris and London. Print the dictionary.
country_capitals = {
'Japan': 'Tokyo',
'France': 'Paris',
'England': 'London'
}
print(country_capitals)
Create a dictionary using curly brackets such that it maps the names of three fruits, Apple, Banana, and Grapes, to their colors: Red, Yellow, and Purple. Print the dictionary.
fruit_colors = {
'Apple': 'Red',
'Banana': 'Yellow',
'Grapes': 'Purple'
}
print(fruit_colors)
18.5.2. Making a dictionary from a list of lists
["New South Wales", "Sydney"]
"New South Wales": "Sydney"
state_capitals = dict([
["New South Wales", "Sydney"],
["Victoria", "Melbourne"],
["Queensland", "Brisbane"]
])
print(state_capitals)
# Output is {'New South Wales': 'Sydney', 'Victoria': 'Melbourne', 'Queensland': 'Brisbane'}
Tasks
Create a dictionary using the dict function and a list of lists such that it maps the names of three programming languages, Python, Java, and C++, to their creators: Guido van Rossum, James Gosling, and Bjarne Stroustrup. Print the dictionary.
Create a dictionary using the dict function and a list of lists such that it maps the names of three countries, China, India, and USA, to their populations in billions: 1.4, 1.4, and 0.3. Print the dictionary.
Create a dictionary using the dict function and a list of lists such that it maps the names of three programming languages, Python, Java, and C++, to their creators: Guido van Rossum, James Gosling, and Bjarne Stroustrup. Print the dictionary.
languages = dict([
['Python', 'Guido van Rossum'],
['Java', 'James Gosling'],
['C++', 'Bjarne Stroustrup']
])
print(languages)
Create a dictionary using the dict function and a list of lists such that it maps the names of three countries, China, India, and USA, to their populations in billions: 1.44, 1.39, and 0.33. Print the dictionary.
populations = dict([
['China', 1.44],
['India', 1.39],
['USA', 0.33]
])
print(populations)
18.5.3. Making a dictionary from a list of tuples
("New South Wales", "Sydney")
"New South Wales": "Sydney"
capitals = dict([
("South Australia", "Adelaide"),
("Western Australia", "Perth"),
("Australian Capital Territory", "Canberra")
])
print(capitals)
# Output is {'South Australia': 'Adelaide', 'Western Australia': 'Perth', 'Australian Capital Territory': 'Canberra'}
Tasks
Create a dictionary using the dict function and a list of tuples such that it maps the names of three car brands, Toyota, BMW, and Ford, to their countries of origin: Japan, Germany, and USA. Print the dictionary.
Create a dictionary using the dict function and a list of tuples such that it maps the names of three planets, Mercury, Venus, and Earth, to their average distances from the sun in million kilometers: 57.9, 108.2, and 149.6. Print the dictionary.
Create a dictionary using the dict function and a list of tuples such that it maps the names of three car brands, Toyota, BMW, and Ford, to their countries of origin: Japan, Germany, and USA. Print the dictionary.
car_brand_countries = dict([
('Toyota', 'Japan'),
('BMW', 'Germany'),
('Ford', 'USA')
])
print(car_brand_countries)
Create a dictionary using the dict function and a list of tuples such that it maps the names of three planets, Mercury, Venus, and Earth, to their average distances from the sun in million kilometers: 57.9, 108.2, and 149.6. Print the dictionary.
planet_distances_to_sun = dict([
('Mercury', 57.9),
('Venus', 108.2),
('Earth', 149.6)
])
print(planet_distances_to_sun)
18.5.4. Making a dictionary from 2 lists
18.5.4.1. Making a dictionary from 2 lists –update
states = ["Queensland", "South Australia", "Western Australia"]
cities = ["Brisbane", "Adelaide", "Perth"]
capitals = {}
for i in range(len(states)):
capitals.update({states[i]: cities[i]})
print(capitals)
# Output is {'Queensland': 'Brisbane', 'South Australia': 'Adelaide', 'Western Australia': 'Perth'}
Tasks
Create a dictionary using the update method and two lists such that it maps the names of ‘Lockett’, ‘Coventry’, and ‘Dunstall’, to their goals kicked: 1360, 1299, and 1254. Print the dictionary.
Create a dictionary using the update method and two lists such that it maps the names of ‘Lockett’, ‘Coventry’, and ‘Dunstall’, to their goals kicked: 1360, 1299, and 1254. Print the dictionary.
names = ['Lockett', 'Coventry', 'Dunstall']
goals = [1360, 1299, 1254]
goal_kickers = {}
for i in range(len(names)):
goal_kickers.update({names[i]: goals[i]})
print(my_dict)
18.5.4.2. Making a dictionary from 2 lists –set key value
states = ["Queensland", "South Australia", "Western Australia"]
cities = ["Brisbane", "Adelaide", "Perth"]
capitals = {}
for i in range(len(states)):
capitals[states[i]] = cities[i]
print(capitals)
# Output is {'Queensland': 'Brisbane', 'South Australia': 'Adelaide', 'Western Australia': 'Perth'}
Tasks
Create a dictionary using the update method and two lists such that it maps the names of ‘Lockett’, ‘Coventry’, and ‘Dunstall’, to their goals kicked: 1360, 1299, and 1254. Print the dictionary.
Create a dictionary using the update method and two lists such that it maps the names of ‘Lockett’, ‘Coventry’, and ‘Dunstall’, to their goals kicked: 1360, 1299, and 1254. Print the dictionary.
names = ['Lockett', 'Coventry', 'Dunstall']
goals = [1360, 1299, 1254]
goal_kickers = {}
for i in range(len(names)):
goal_kickers[names[i]] = goals[i]
print(goal_kickers)
18.5.4.3. Making a dictionary from 2 lists –dict and zip
('Queensland', 'Brisbane'), ('South Australia', 'Adelaide'), ('Western Australia', 'Perth')
states = ["Queensland", "South Australia", "Western Australia"]
cities = ["Brisbane", "Adelaide", "Perth"]
capitals = dict(zip(states, cities))
print(capitals)
# Output is {'Queensland': 'Brisbane', 'South Australia': 'Adelaide', 'Western Australia': 'Perth'}
Tasks
Create a dictionary using the zip function and two lists such that it maps the names of three animals, Elephant, Dog, and Cat, to their average lifespans in years: 70, 13, and 15. Print the dictionary.
Create a dictionary using the zip function and two lists such that it maps the names of three cities, Tokyo, Delhi, and Shanghai, to their populations in millions: 37.4, 28.5, and 25.6. Print the dictionary.
Create a dictionary using the zip function and two lists such that it maps the names of three animals, Elephant, Dog, and Cat, to their average lifespans in years: 70, 13, and 15. Print the dictionary.
animals = ['Elephant', 'Dog', 'Cat']
lifespans = [70, 13, 15]
animal_lifespans = dict(zip(animals, lifespans))
print(animal_lifespans)
Create a dictionary using the zip function and two lists such that it maps the names of three cities, Tokyo, Delhi, and Shanghai, to their populations in millions: 37.4, 28.5, and 25.6. Print the dictionary.
cities = ['Tokyo', 'Delhi', 'Shanghai']
populations = [37.4, 28.5, 25.6]
city_populations = dict(zip(cities, populations))
print(city_populations)
18.5.5. Making a dictionary by dictionary comprehension from 2 lists
states = ["Western Australia", "Tasmania", "Northern Territory"]
cities = ["Perth", "Hobart", "Darwin"]
capitals = {states[i]: cities[i] for i in range(len(states))}
print(capitals)
Tasks
Create a dictionary using a dictionary comprehension via the indexes of two lists such that it maps the names of ‘Lockett’, ‘Coventry’, and ‘Dunstall’, to their goals kicked: 1360, 1299, and 1254. Print the dictionary.
Create a dictionary using a dictionary comprehension via the indexes of two lists such that it maps the names of ‘Lockett’, ‘Coventry’, and ‘Dunstall’, to their goals kicked: 1360, 1299, and 1254. Print the dictionary.
names = ['Lockett', 'Coventry', 'Dunstall']
goals = [1360, 1299, 1254]
goal_kickers = {names[i]: goals[i] for i in range(len(names))}
print(goal_kickers)
states = ["Western Australia", "Tasmania", "Northern Territory"]
cities = ["Perth", "Hobart", "Darwin"]
capitals = {state: city for state, city in zip(states, cities)}
print(capitals)
Tasks
Create a dictionary using dictionary comprehension and two lists such that it maps the names of three sports, Soccer, Basketball, and Baseball, to the number of players in each team: 11, 5, and 9. Print the dictionary.
Create a dictionary using dictionary comprehension and two lists such that it maps the names of three countries, USA, China, and Japan, to their GDPs in trillion USD: 21.43, 14.34, and 5.08. Print the dictionary.
Create a dictionary using dictionary comprehension and two lists such that it maps the names of three sports, Soccer, Basketball, and Baseball, to the number of players in each team: 11, 5, and 9. Print the dictionary.
sports = ['Soccer', 'Basketball', 'Baseball']
players = [11, 5, 9]
sport_players = {sport: player for sport, player in zip(sports, players)}
print(sport_players)
Create a dictionary using dictionary comprehension and two lists such that it maps the names of three countries, USA, China, and Japan, to their GDPs in trillion USD: 21.43, 14.34, and 5.08. Print the dictionary.
countries = ['USA', 'China', 'Japan']
gdps = [21.43, 14.34, 5.08]
country_gdps = {country: gdp for country, gdp in zip(countries, gdps)}
print(country_gdps)
18.5.6. Making a dictionary from key word arguments
a=1
will become the key value pair 'a': 1
simple_dict = dict(a=1, b=2, c=3, d=4)
print(simple_dict)
# Output is {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Tasks
Create a dictionary using keyword arguments such that it maps the names of three programming languages, Python, Java, and JavaScript, to their release years: 1991, 1995, and 1995. Print the dictionary.
Create a dictionary using keyword arguments such that it maps the names of three continents, Africa, Asia, and Europe, to their areas in million square kilometers: 30.37, 44.58, and 10.18. Print the dictionary.
Create a dictionary using keyword arguments such that it maps the names of three programming languages, Python, Java, and JavaScript, to their release years: 1991, 1995, and 1995. Print the dictionary.
languages_release_years = dict(Python=1991, Java=1995, JavaScript=1995)
print(languages_release_years)
# Output is {'Python': 1991, 'Java': 1995, 'JavaScript': 1995}
Create a dictionary using keyword arguments such that it maps the names of three continents, Africa, Asia, and Europe, to their areas in million square kilometers: 30.37, 44.58, and 10.18. Print the dictionary.
continents = dict(Africa=30.37, Asia=44.58, Europe=10.18)
print(continents)
# Output is {'Africa': 30.37, 'Asia': 44.58, 'Europe': 10.18}