Exercise 6-7 Solution Python Crash Course Chapter 6: Dictionaries

 

πŸ“˜ 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:

  1. 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"
  2. Store dictionaries in a list:

    The three dictionaries are stored in a list called people. This allows for easy iteration through all the dictionaries.

  3. Iterate through the list of dictionaries:

    A for loop iterates through each dictionary in the people list. Each dictionary represents a person.

  4. Iterate through key-value pairs:

    For each dictionary, the items() method is used to retrieve all key-value pairs. A nested for loop iterates through these pairs, where i represents the key (e.g., "firstname") and j represents the value (e.g., "John").

  5. 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:


πŸ”— 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

“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