0
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.
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Jenn
  • 11
  • 2
  • Please go through the [intro tour](https://stackoverflow.com/tour), the [help center](https://stackoverflow.com/help) and [how to ask a good question](https://stackoverflow.com/help/how-to-ask) to see how this site works and to help you improve your current and future questions, which can help you get better answers. "Show me how to solve this coding problem?" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a *specific* question about your implementation. – Prune Mar 21 '21 at 00:17
  • 1
    Stack Overflow is not intended to replace existing tutorials and documentation. If you do not know how to iterate through a dict or access its elements, you need to repeat your tutorial on dicts. – Prune Mar 21 '21 at 00:18
  • `people[key]` → `list` with two elements, `list[0]` and `list[1]`. – martineau Mar 21 '21 at 01:27
  • Noted. I'll be better at asking questions on here. – Jenn Mar 21 '21 at 02:51

2 Answers2

3

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]}")
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
  • Thank you so much! I merged two dictionaries to obtain my people list and I wasn't sure how to access it prior to this. – Jenn Mar 21 '21 at 02:45
1

You can also do that:

for first, (cat, room) in people.items():
  print(f"{first} is the {cat} of a {room} room")
DevLounge
  • 8,313
  • 3
  • 31
  • 44