Jstuff
Jstuff

Reputation: 1342

Loop returns false after random number of iteration in python

In the below code

import pyzipcode as pyzip
location = []
for var in grouped_list_long_zip:
    holder = pyzip.Pyzipcode.get(var[0][4], 'US', return_json=False)
    location.append(holder['location'])

grouped_list_long_zip is a list of list of list which contains locations where the fourth index in each sublist is the zipcode. Using this and the module pyzipcode, I want to return latitude and longitude location of the zipcodes and store them in location. My problem is after a random number of iteration the function will return false and make holder a bool type. The most common iteration it fails on is the 11th, but it also happens on other iterations. I am unsure of how to debug this problem.

Edit:

When the code fails var simply equals what grouped_lsit_long_zip equals at that location.

Upvotes: 0

Views: 85

Answers (1)

Ma0
Ma0

Reputation: 15204

I would modify the code like that to get some feedback on the values that disrupt the execution.

import pyzipcode as pyzip
location = []

for var in grouped_list_long_zip:
    holder = pyzip.Pyzipcode.get(var[0][4], 'US', return_json=False)
    if type(holder) == bool:
        print(var[0])
    else:
        location.append(holder['location'])

Another thing you can do is wrapping the thing in a try-except block to simply ignore the problematic cases if an error is thrown.

Upvotes: 1

Related Questions