-3

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")
Mureinik
  • 297,002
  • 52
  • 306
  • 350
furby1108
  • 3
  • 2
  • 2
    Read the [official Python tutorial](https://docs.python.org/3/tutorial/index.html). Read it. Don't skim it. That's not how for loops work. Or how `dict.values` works. Guessing and checking to figure out how code works is not going to work. You need to understand the language before trying to solve real problems in it. – Silvio Mayolo Sep 12 '21 at 19:57
  • Your nested loop is not necessary because of the way you can access dictionaries with their keys. Do check my explanation and hopefully, it would be enough to guide you in how to work with nested data structures. – Akshay Sehgal Sep 12 '21 at 20:18
  • Does this answer your question? [Iterating over dictionaries using 'for' loops](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops) – bad_coder Sep 12 '21 at 20:23

3 Answers3

2

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')
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

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)

Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
0

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']
balderman
  • 22,927
  • 7
  • 34
  • 52