I have a list of 4 items (user ids). I want to iterate through the list and pass each user id to a function. This function would do a request to an api for each user, return json response and create list, and then create a list for each user's individual data. These four new lists would be passed to a new function.
However, I am getting the error UnboundLocalError: local variable 'userid2_list' referenced before assignment
.
How can I ensure the iteration is completed and a list generated for each user id before the function is called?
userid_list = [1, 2, 3, 4]
for userid in userid_list:
get_info(userid)
###
def get_info(userid):
response = requests.get(f"www.example.com/api/{userid}")
data_list = list(response.json()['a']['b'])
if userid == 1:
userid1_list = data_list
elif userid == 2:
userid2_list = data_list
elif userid == 3:
userid3_list = data_list
if userid == 4:
userid4_list = data_list
use_info(userid1_list, userid2_list, userid3_list, userid4_list)