Creating a Dictionary
Part of the Logic & Flow section of Coddy's Python journey — lesson 8 of 78.
To create a dictionary in Python, you use curly braces {} and specify the key-value pairs within them. Each key-value pair is written as key: value, and multiple pairs are separated by commas.
Here's how you can create a dictionary:
# Creating a dictionary of country capitals
country_capitals = {
"USA": "Washington, D.C.",
"France": "Paris",
"Japan": "Tokyo"
}
# Creating a dictionary of employee information
employee = {
"name": "John Doe",
"age": 30,
"position": "Software Engineer"
}
# Creating an empty dictionary
empty_dict = {}In the first example, country_capitals is a dictionary with country names as keys and their capitals as values. In the second example, employee is a dictionary containing information about an employee. The third example shows how to create an empty dictionary, which can be useful when you want to add items to it later.
Challenge
EasyCreate a function named create_student_dict that takes three parameters: name, age, and major. The function should return a dictionary where the keys are "name", "age", and "major", and the values are the corresponding values passed to the function.
For example, calling create_student_dict("Alice", 20, "Computer Science") should return a dictionary with the following key-value pairs:
- Key:
"name", Value:"Alice" - Key:
"age", Value:20 - Key:
"major", Value:"Computer Science"
Cheat sheet
To create a dictionary in Python, use curly braces {} with key-value pairs separated by commas:
# Dictionary with key-value pairs
country_capitals = {
"USA": "Washington, D.C.",
"France": "Paris",
"Japan": "Tokyo"
}
# Dictionary with different data types
employee = {
"name": "John Doe",
"age": 30,
"position": "Software Engineer"
}
# Empty dictionary
empty_dict = {}Each key-value pair is written as key: value, where keys are typically strings and values can be any data type.
Try it yourself
def create_student_dict(name, age, major):
# Write code hereThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter