Home > Software design >  Return Values on List of Dictionaries [closed]
Return Values on List of Dictionaries [closed]

Time:09-17

I have the following list of dictionaries:

voting_data = [ {"county":"Arapahoe", "registered_voters": 422829}, {"county":"Denver", "registered_voters": 463353}, {"county":"Jefferson", "registered_voters": 432438}]

I need to create a for-loop that, when executed, will produce the following sentences:

"Arapahoe County has 422,829 registered voters" (and so on).

I've been stuck on this all day, and have tried different loops/variables. I can't wrap my head around it. I understand that I'm looking to retrieve the index values of each item (so, for "county":"Arapahoe" I'm looking to retrieve "Arapahoe" and for "registered_voters": 422829, "422829").

Most recently, I came up with:

for counties_dict in voting_data:
    for i in counties_dict.values("county") and j in counties_dict.values("registered_voters"):
        print(f" {i} county has {j:,} registered voters")

CodePudding user response:

You don't need a nested loop. You need to loop over the list, and access each of the dict's properties:

for vd in voting_data:
    print(f'{vd["county"]} has {vd["registered_voters"]} registered voters')

CodePudding user response:

You can do this with dict.get() to fetch values for specific keys.

for d in voting_data:
    county = d.get('county')
    voters = '{:,}'.format(d.get('registered_voters'))
    print(f'{county} county has {voters} registered voters.')
    
Arapahoe county has 422,829 registered voters.
Denver county has 463,353 registered voters.
Jefferson county has 432,438 registered voters.    

NOTE: '{:,}'.format(100000) results in formatting the number as 100,000 and returns a string which can be printed in the format you are looking for.


It's important to understand how nested data structures behave. You can iterate over a list of objects using for-loop

for item in list:
    print(item)

In this case, the items are dictionaries. In order to access a dictionary (key, value pairs), you can either access values directly from their corresponding keys.

d = {'k1':'v1', 
     'k2':'v2'}

>>> d['k1']
v1

#OR

>>> d.get('k1')
v1

Incase you want to iterate over a dictionary (key and value pairs), THEN you would need an additional for loop.

for k,v in d.items():
    print(k, v)
(k1,v1)
(k2,v2)

Hope that clarifies why you dont need a nested loop in your case. Since you have a list of dictionaries, you can iterate over the list and then access each dictionary with its specific keys (which in this case are county and registered_voters)

CodePudding user response:

something like the below (1 liner)

voting_data = [ {"county":"Arapahoe", "registered_voters": 422829}, {"county":"Denver", "registered_voters": 463353}, {"county":"Jefferson", "registered_voters": 432438}]
output = [f'{x["county"]} County has {x["registered_voters"]:,} registered voters' for x in voting_data]
print(output)

output

['Arapahoe County has 422,829 registered voters', 'Denver County has 463,353 registered voters', 'Jefferson County has 432,438 registered voters']
  • Related