Home > Software engineering >  Iterate through list of dictionaries and print message
Iterate through list of dictionaries and print message

Time:10-14

I have a list of dictionaries that looks similar to following:

points = [
{"callsign": "A001", "daily_points":10, "number":" 447774076621"},
{"callsign": "A002", "daily_points":5, "number":" 447958708481"}
]

I am trying to iterate through list to print message like: "Hi A001 you received 10 points today"

I have tried using following code:

for callsign, daily_points in points.items():
print(f"Hi {callsign} you recieved {daily_points} points today.")

But it returns: AttributeError: 'list' object has no attribute 'items'

What am I doing wrong?

CodePudding user response:

points is a list, not a dictionary. You have to loop through each element in the list, then read the dictionary. The code looks somehow like this:

for point in points:
    print(f"Hi {point['callsign']} you received {point['daily_points']} points today.")

CodePudding user response:

You can use str.format:

for point in points:
    print("Hi {callsign} you recieved {daily_points} points today.".format(**point))

Also since in python 3.6 dict's preserve order you can keep most your code just replace points.items() with map(dict.values, points):

for callsign, daily_points, *_ in map(dict.values, points):
    print(f"Hi {callsign} you recieved {daily_points} points today.")

CodePudding user response:

The variable 'points' is a list of dictionaries, not a dictionary itself. To loop over each dictionary in the list, you could:

points = [
{"callsign": "A001", "daily_points":10, "number":" 447774076621"},
{"callsign": "A002", "daily_points":5, "number":" 447958708481"}
]

for point in points:
    print(f"Hi {point['callsign']} you recieved {point['daily_points']} points today.")

This would iterate through the list, and print out the information in each dictionary.

CodePudding user response:

You can try this:

for point in points:
    callsign, daily_points, number = point['callsign'], point['daily_points'], point['number']
    print(f"Hi {callsign} you recieved {daily_points} points today.")

CodePudding user response:

You could iterate through every record in points and do this.

for i in range(len(points)):
    print(f"Hi {points[i]['callsign']} you recieved {points[i]['daily_points']} points today.")

Output

Hi A001 you recieved 10 points today.
Hi A002 you recieved 5 points today.

CodePudding user response:

The easiest solution looks like this:

for person in points:
    print(f"Hi {person['callsign']} you recieved {person['daily_points']} points today.")

Your solution is wrong because list does not have method items(), and it can not unpack dictionary keys like that.

CodePudding user response:

The problem is that you are iterating over points, that is a list and has no items attribute.

One approach is to use .format and pass the dictionary directly:

points = [
    {"callsign": "A001", "daily_points": 10, "number": " 447774076621"},
    {"callsign": "A002", "daily_points": 5, "number": " 447958708481"}
]

for d in points:
    print("Hi {callsign} you received {daily_points} points today.".format(**d))

Output

Hi A001 you received 10 points today.
Hi A002 you received 5 points today.

CodePudding user response:

you need to access the value with the key

for user in points:
    print(f"Hi {user['callsign']} you recieved {user['daily_points']} points today.")

CodePudding user response:

The problem is the items() method is for lists only, not for dictionaries. However, you could try something like this:

for dictionary in points:
    print(f"Hi {dictionary['callsign']} you recieved {dictionary['daily_points']} points today.")

Or you can use the get() method as follows:

for dictionary in points:
    print(f"Hi {dictionary.get('callsign')} you recieved {dictionary.get('daily_points')} points today.")

CodePudding user response:

You mix dict and list..

See below

points = [{
    "callsign": "A001",
    "daily_points": 10,
    "number": " 447774076621"
}, {
    "callsign": "A002",
    "daily_points": 5,
    "number": " 447958708481"
}]

for point in points:
    print(
        f'Hi {point.get("callsign")} you received {point.get("daily_points")} points today.'
    )

output

Hi A001 you received 10 points today.
Hi A002 you received 5 points today.
  • Related