Course Content
Python Data Structures
Python Data Structures
Creating a Dictionary
A dictionary is a data structure that stores unordered key-value pairs.
Imagine you need to create a dictionary called student
with these pairs:
Key | Value |
'first name' | 'Ann' |
'last name' | 'Elliot' |
'city' | 'New York' |
Check out the code to make this dictionary.
student = {'first name': 'Ann', 'last name': 'Elliot', 'city': 'New York'} print(student)
In a dictionary, you can use any immutable data type for a key. For instance, the dictionary we just looked at has strings as keys. Now, let's whip up a dictionary using numbers as keys.
countries = {4: 'Belgium', 2: 'Finland', 7: 'China', 5: 'Cyprus'} print(countries)
Remember, the value associated with a specific key can be anything — strings, numbers, lists, and so on.
It's time to practice!
Task
Your task is to craft this dictionary:
'Colorado':'Rockies', 'Boston':'Red Sox', 'Minnesota':'Twins'
.
Here, Colorado
is a key, while Rockies
is its corresponding value, and so forth.
Thanks for your feedback!
Creating a Dictionary
A dictionary is a data structure that stores unordered key-value pairs.
Imagine you need to create a dictionary called student
with these pairs:
Key | Value |
'first name' | 'Ann' |
'last name' | 'Elliot' |
'city' | 'New York' |
Check out the code to make this dictionary.
student = {'first name': 'Ann', 'last name': 'Elliot', 'city': 'New York'} print(student)
In a dictionary, you can use any immutable data type for a key. For instance, the dictionary we just looked at has strings as keys. Now, let's whip up a dictionary using numbers as keys.
countries = {4: 'Belgium', 2: 'Finland', 7: 'China', 5: 'Cyprus'} print(countries)
Remember, the value associated with a specific key can be anything — strings, numbers, lists, and so on.
It's time to practice!
Task
Your task is to craft this dictionary:
'Colorado':'Rockies', 'Boston':'Red Sox', 'Minnesota':'Twins'
.
Here, Colorado
is a key, while Rockies
is its corresponding value, and so forth.
Thanks for your feedback!
Creating a Dictionary
A dictionary is a data structure that stores unordered key-value pairs.
Imagine you need to create a dictionary called student
with these pairs:
Key | Value |
'first name' | 'Ann' |
'last name' | 'Elliot' |
'city' | 'New York' |
Check out the code to make this dictionary.
student = {'first name': 'Ann', 'last name': 'Elliot', 'city': 'New York'} print(student)
In a dictionary, you can use any immutable data type for a key. For instance, the dictionary we just looked at has strings as keys. Now, let's whip up a dictionary using numbers as keys.
countries = {4: 'Belgium', 2: 'Finland', 7: 'China', 5: 'Cyprus'} print(countries)
Remember, the value associated with a specific key can be anything — strings, numbers, lists, and so on.
It's time to practice!
Task
Your task is to craft this dictionary:
'Colorado':'Rockies', 'Boston':'Red Sox', 'Minnesota':'Twins'
.
Here, Colorado
is a key, while Rockies
is its corresponding value, and so forth.
Thanks for your feedback!
A dictionary is a data structure that stores unordered key-value pairs.
Imagine you need to create a dictionary called student
with these pairs:
Key | Value |
'first name' | 'Ann' |
'last name' | 'Elliot' |
'city' | 'New York' |
Check out the code to make this dictionary.
student = {'first name': 'Ann', 'last name': 'Elliot', 'city': 'New York'} print(student)
In a dictionary, you can use any immutable data type for a key. For instance, the dictionary we just looked at has strings as keys. Now, let's whip up a dictionary using numbers as keys.
countries = {4: 'Belgium', 2: 'Finland', 7: 'China', 5: 'Cyprus'} print(countries)
Remember, the value associated with a specific key can be anything — strings, numbers, lists, and so on.
It's time to practice!
Task
Your task is to craft this dictionary:
'Colorado':'Rockies', 'Boston':'Red Sox', 'Minnesota':'Twins'
.
Here, Colorado
is a key, while Rockies
is its corresponding value, and so forth.