Printing the elements of a dictionary that is contained in a list HOW?

Hi, I have tried multiple ways to print this but came up stumped:
I want to print the key value pairs in the following dictionary, I can do it with no problems formatting it just as a dictionary, however I am stumped trying to do it from a list. I can print individual entries but can’t get further than that with a loop etc… any help apreciated.

users = [{‘index’: 3, ‘name’:‘Dave’, ‘town’:‘Bham’}, {‘index’: 2, ‘name’:‘Alan’, ‘town’:‘Manchester’}]

so that the output looks like this:

index: 3
name: Dave
town: Bham

index: 2
name: Alan
town: Manchester

Hi, I have tried multiple ways to print this but came up stumped:
I want to print the key value pairs in the following dictionary, I can do it with no problems formatting it just as a dictionary, however I am stumped trying to do it from a list. I can print individual entries but can’t get further than that with a loop etc… any help apreciated.

users = [{‘index’: 3, ‘name’:‘Dave’, ‘town’:‘Bham’}, {‘index’: 2, ‘name’:‘Alan’, ‘town’:‘Manchester’}]

If you’ve got a simple function:

def print_dict(d):
    ... print a single dictionary ...

so that the output looks like this:

index: 3
name: Dave
town: Bham

You can do that in a loop like this:

for entry in your_list:
    print_dict(entry)

You could compute the index yourself:

i = 0
for entry in your_list:
    print("index:", i)
    print_dict(entry)
    i += 1

Python has a handy enumerate function to do that for you:

for i, entry in enumerate(your_list):
    print("index:", i)
    print_dict(entry)

because this is very common.

Adjust to suit.

Cheers,
Cameron Simpson cs@cskk.id.au

I always prefer that problem is accompanied with some code. Nevertheless, this problem can be solved by using built-in print() function: `iterate over dictionaries in list and print key value pairs in dictionary separated with newline and separate dictionaries with empty line’

>>> for record in users:
...     print(*(f'{k}: {v}' for k, v in record.items()), sep='\n', end='\n\n')
...
index: 3
name: Dave
town: Bham

index: 2
name: Alan
town: Manchester

Note that this will add empty line at the end of last dictionary as well.

I like pprint.

from pprint import pprint
>>> pprint(users, width=80)
[{'index': 3, 'name': 'Dave', 'town': 'Bham'},
 {'index': 2, 'name': 'Alan', 'town': 'Manchester'}]
>>> pprint(users, width=40)
[{'index': 3,
  'name': 'Dave',
  'town': 'Bham'},
 {'index': 2,
  'name': 'Alan',
  'town': 'Manchester'}]

hi Aivar, thankyou so much for this. I didn’t put any code, I had tried several ways but I am very new to this and whilst some of my attempts looked similar to solution here they were way off. I guess you could call it “not wishing to be embarrassed”! but I am overwhelmed by peoples patience and responses so thanks again to you and everyone for the help!

Hi Kazuya,

thankyou - I am going to try all these methods and add them to my notes. You probably guessed I am a beginner, I had tried myself but my code wasn’t very productive, I have been able to learn a lot just from reading the responses here. I am grateful for all the responses recieved!!