Home > Net >  Python Problem - For Loop not behaving as expected
Python Problem - For Loop not behaving as expected

Time:12-12

import requests, json
zip_code = "85215"
distance = "20"

#list of zip codes in an area
zipcodeData = {'zip_codes': [{'zip_code': '85140', 'distance': 18.585, 'city': 'San Tan Valley', 'state': 'AZ'}, {'zip_code': '85297', 'distance': 18.395, 'city': 'Gilbert', 'state': 'AZ'}, {'zip_code': '85295', 'distance': 17.185, 'city': 'Gilbert', 'state': 'AZ'}, {'zip_code': '85225', 'distance': 19.748, 'city': 'Chandler', 'state': 'AZ'}, {'zip_code': '85217', 'distance': 19.996, 'city': 'Apache Junction', 'state': 'AZ'}, {'zip_code': '85212', 'distance': 13.478, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85299', 'distance': 16.319, 'city': 'Gilbert', 'state': 'AZ'}, {'zip_code': '85236', 'distance': 14.654, 'city': 'Higley', 'state': 'AZ'}, {'zip_code': '85296', 'distance': 15.819, 'city': 'Gilbert', 'state': 'AZ'}, {'zip_code': '85227', 'distance': 12.227, 'city': 'Chandler Heights', 'state': 'AZ'}, {'zip_code': '85218', 'distance': 14.859, 'city': 'Apache Junction', 'state': 'AZ'}, {'zip_code': '85233', 'distance': 17.287, 'city': 'Gilbert', 'state': 'AZ'}, {'zip_code': '85234', 'distance': 13.947, 'city': 'Gilbert', 'state': 'AZ'}, {'zip_code': '85209', 'distance': 10.025, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85202', 'distance': 19.335, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85274', 'distance': 19.066, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85210', 'distance': 17.409, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85214', 'distance': 14.304, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85216', 'distance': 11.681, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85204', 'distance': 14.374, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85206', 'distance': 11.254, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85219', 'distance': 10.365, 'city': 'Apache Junction', 'state': 'AZ'}, {'zip_code': '85118', 'distance': 15.16, 'city': 'Gold Canyon', 'state': 'AZ'}, {'zip_code': '85120', 'distance': 7.594, 'city': 'Apache Junction', 'state': 'AZ'}, {'zip_code': '85208', 'distance': 8.01, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85119', 'distance': 8.352, 'city': 'Apache Junction', 'state': 'AZ'}, {'zip_code': '85220', 'distance': 7.061, 'city': 'Apache Junction', 'state': 'AZ'}, {'zip_code': '85278', 'distance': 7.398, 'city': 'Apache Junction', 'state': 'AZ'}, {'zip_code': '85117', 'distance': 6.746, 'city': 'Apache Junction', 'state': 'AZ'}, {'zip_code': '85178', 'distance': 6.746, 'city': 'Apache Junction', 'state': 'AZ'}, {'zip_code': '85205', 'distance': 9.723, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85201', 'distance': 16.532, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85211', 'distance': 15.824, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85213', 'distance': 11.768, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85275', 'distance': 11.245, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85277', 'distance': 9.15, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85290', 'distance': 5.169, 'city': 'Apache Junction', 'state': 'AZ'}, {'zip_code': '85203', 'distance': 13.561, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85207', 'distance': 5.197, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85271', 'distance': 19.385, 'city': 'Scottsdale', 'state': 'AZ'}, {'zip_code': '85257', 'distance': 19.718, 'city': 'Scottsdale', 'state': 'AZ'}, {'zip_code': '85252', 'distance': 19.733, 'city': 'Scottsdale', 'state': 'AZ'}, {'zip_code': '85261', 'distance': 19.667, 'city': 'Scottsdale', 'state': 'AZ'}, {'zip_code': '85251', 'distance': 19.616, 'city': 'Scottsdale', 'state': 'AZ'}, {'zip_code': '85256', 'distance': 15.237, 'city': 'Scottsdale', 'state': 'AZ'}, {'zip_code': '85215', 'distance': 0, 'city': 'Mesa', 'state': 'AZ'}, {'zip_code': '85190', 'distance': 11.499, 'city': 'Tortilla Flat', 'state': 'AZ'}, {'zip_code': '85250', 'distance': 17.791, 'city': 'Scottsdale', 'state': 'AZ'}, {'zip_code': '85258', 'distance': 18.57, 'city': 'Scottsdale', 'state': 'AZ'}, {'zip_code': '85269', 'distance': 9.677, 'city': 'Fountain Hills', 'state': 'AZ'}, {'zip_code': '85259', 'distance': 14.594, 'city': 'Scottsdale', 'state': 'AZ'}, {'zip_code': '85268', 'distance': 11.462, 'city': 'Fountain Hills', 'state': 'AZ'}, {'zip_code': '85267', 'distance': 19.121, 'city': 'Scottsdale', 'state': 'AZ'}, {'zip_code': '85260', 'distance': 19.201, 'city': 'Scottsdale', 'state': 'AZ'}, {'zip_code': '85264', 'distance': 8.931, 'city': 'Fort Mcdowell', 'state': 'AZ'}, {'zip_code': '85255', 'distance': 17.667, 'city': 'Scottsdale', 'state': 'AZ'}]}
#print(type(zipcodeData))

#goal is to create list of zip codes from dictionary
for i in zipcodeData:
    zipcodeList = zipcodeData["zip_codes"][i]['zip_code'].append()
    print(zipcodeList)

I am trying to take the dictionary and cycle through it to make a list of all the "zip_code" values. However, I'm getting the error "TypeError: list indices must be integers or slices, not str". Done some googling and not sure as I'm following documentation for creating the loop. I'm sure it is something I'm overlooking so any assistance finding the solution would be great!

Tried googling, reading up on the error, looking at examples, playing with the code.

CodePudding user response:

If you want to add all zip_codes from the zipcodeData to a list you can use next example:

zipcodeList = []
for c in zipcodeData["zip_codes"]:
    zipcodeList.append(c["zip_code"])

print(zipcodeList)

Prints:

[
    "85140",
    "85297",
    "85295",
    "85225",
    "85217",

...and so on.

CodePudding user response:

for i in zipcodeData:
    zipcodeList = zipcodeData["zip_codes"][i]['zip_code'].append()

Iterating over a dictionary returns the keys.

The first key in zipcodeData is "zip_codes", so the above loop is trying to do:

zipcodeList = zipcodeData["zip_codes"]["zip_codes"]['zip_code'].append()

... which is clearly wrong.

CodePudding user response:

The zipcodeData is a dict, I think you want to Iterate over the list in the zipcodeData,which is zipcodeData["zip_codes"],and every element in zipcodedata list is a dict.

So the code would be this:

#goal is to create list of zip codes from dictionary
zipcodeList=[]
for zipcode in zipcodeData["zip_codes"]:
    zipcodeList.append(zipcode['zip_code'])
    print(zipcodeList)
  • Related