Reputation: 11
people = {"Jenn" : ['renter', 'large room'],
"Lana" : ['renter', 'small room'],
"Ricky" :['owner', 'large room']
}
Is there a way to access each individual value for a given key via for loop to print the stats of each person? I'm relatively new to Python and I'm having troubles searching for this exact scenario. I'm familiar with f-string formatting.
Expected output for print()
or sys.stderr.write()
Jenn is the renter of a large room.
Lana is the renter of a small room.
Rickey is the owner of a large room.
Upvotes: 0
Views: 104
Reputation: 5387
Use dict.items()
to loop through (key, value)
tuples:
for key, value in people.items():
print(f"{key} is the {value[0]} of a {value[1]}")
Upvotes: 3
Reputation: 8437
You can also do that:
for first, (cat, room) in people.items():
print(f"{first} is the {cat} of a {room} room")
Upvotes: 1