π Python Crash Course – Chapter 6: Dictionaries – Exercise 6-7 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 6-7
Task:
Start with the program you wrote for Exercise 6-1 .
Make two new dictionaries representing different people, and store all three
dictionaries in a list called people .
Loop through your list of people . As you
loop through the list, print everything you know about each person
✅ Solution Code:
# I will Create Dict With Different People Names
p1 = {"firstname": "John", "lastname": "Doe", "age": 30, "city": "New York"}
p2 = {"firstname": "Jane", "lastname": "Smith", "age": 25, "city": "Los Angeles"}
p3 = {"firstname": "Bob", "lastname": "Johnson", "age": 40, "city": "Chicago"}
people =[p1,p2,p3]
for person in people:
for i,j in person.items():
print(f'{i} : {j}')
print("\n")
π§ Code Explanation:
This Python code demonstrates how to work with a list of dictionaries, where each dictionary represents a person with their details. Here's how it works:
-
Create dictionaries for individuals:
Three dictionaries are created, each representing a person with the following key-value pairs:
"firstname": "John"
,"lastname": "Doe"
,"age": 30
,"city": "New York"
"firstname": "Jane"
,"lastname": "Smith"
,"age": 25
,"city": "Los Angeles"
"firstname": "Bob"
,"lastname": "Johnson"
,"age": 40
,"city": "Chicago"
-
Store dictionaries in a list:
The three dictionaries are stored in a list called
people
. This allows for easy iteration through all the dictionaries. -
Iterate through the list of dictionaries:
A
for
loop iterates through each dictionary in thepeople
list. Each dictionary represents a person. -
Iterate through key-value pairs:
For each dictionary, the
items()
method is used to retrieve all key-value pairs. A nestedfor
loop iterates through these pairs, wherei
represents the key (e.g., "firstname") andj
represents the value (e.g., "John"). -
Display details for each person:
The program displays all the details of each person in a formatted manner. A blank line is added after each person's details for better readability.
This code demonstrates how to organize data using dictionaries and lists, and how to iterate through nested data structures to display their contents.
π Output:
firstname : John
lastname : Doe
age : 30
city : New York
firstname : Jane
lastname : Smith
age : 25
city : Los Angeles
firstname : Bob
lastname : Johnson
age : 40
city : Chicago
π Related Exercises from Chapter 6:
- ➤ Exercise 6-1 | Person
- ➤ Exercise 6-2 | Favorite Numbers
- ➤ Exercise 6-3 | Glossary
- ➤ Exercise 6-4 | Glossary 2
- ➤ Exercise 6-5 | Rivers
- ➤ Exercise 6-6 | Polling
- ➤ Exercise 6-7 | People
- ➤ Exercise 6-8 | Pets
- ➤ Exercise 6-9 | Favorite Places
- ➤ Exercise 6-10 | Favorite Numbers
- ➤ Exercise 6-11 | Cities
- ➤ Exercise 6-12 | Extensions
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”
— Only Python
π Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips
πTrending Topics
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment