Looping through a list of dictionaries

Joined
Dec 12, 2022
Messages
8
Reaction score
0
I am trying to use a for loop to loop through this list of dictionaries and print all the information about each person neatly. This is what I have so far.


Python:
sarah = {
    'age': 50,
    'job': 'mcdonalds',
    'city': 'atlanta',
    }
steven = {
    'age': 24,
    'job': 'target',
    'city': 'new york',
    }
john = {
    'age': 20,
    'job': 'walmart',
    'city': 'new york',
    }

people = [sarah, steven, john]

How do I write a loop that will go through the list and print the details of each person neatly? Thanks
 
Joined
Dec 10, 2022
Messages
73
Reaction score
22
I recommend formatting the dict something like this:

Python:
people = {
    'sarah': {
        'age': 50,
        'job': 'mcdonalds',
        'city': 'atlanta',
        },
    'steven': {
        'age': 24,
        'job': 'target',
        'city': 'new york',
        },
    'john':{
        'age': 20,
        'job': 'walmart',
        'city': 'new york',
        }
}



for person, name in people.items():
    print(f'{person}:')
    for field, data in name.items():
        print(f'{field}: {data}')
    print('\n')

output

Code:
sarah:
age: 50
job: mcdonalds
city: atlanta


steven:
age: 24
job: target
city: new york


john:
age: 20
job: walmart
city: new york

If you want to go with the way you have, I would add a name key, value pair to the dicts.
Python:
sarah = {
    'name': 'sarah',
    'age': 50,
    'job': 'mcdonalds',
    'city': 'atlanta',
    }
steven = {
    'name': 'steven',
    'age': 24,
    'job': 'target',
    'city': 'new york',
    }
john = {
    'name': 'john',
    'age': 20,
    'job': 'walmart',
    'city': 'new york',
    }

people = [sarah, steven, john]

for i, person in enumerate(people):
    for field, data in people[I].items():
        print(f'{field}: {data}')
    print('\n')
 
Last edited:
Joined
Dec 10, 2022
Messages
73
Reaction score
22
Just wanted to throw one more in using tabulate module

Python:
from tabulate import tabulate

sarah = {
    'name': 'sarah',
    'age': 50,
    'job': 'mcdonalds',
    'city': 'atlanta',
    }

steven = {
    'name': 'steven',
    'age': 24,
    'job': 'target',
    'city': 'new york',
    }

john = {
    'name': 'john',
    'age': 20,
    'job': 'walmart',
    'city': 'new york',
    }

headers = {'name':'Name', 'age':'Age', 'job':'Job', 'city':'City'}
people = [sarah, steven, john]


print(tabulate(people, headers=headers, tablefmt='pretty'))

Output

Code:
+--------+-----+-----------+----------+
|  Name  | Age |    Job    |   City   |
+--------+-----+-----------+----------+
| sarah  | 50  | mcdonalds | atlanta  |
| steven | 24  |  target   | new york |
|  john  | 20  |  walmart  | new york |
+--------+-----+-----------+----------+
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top