Determining index of list(zip)

Viewed 71

I am trying to determine which tuple pair in a zip (tax_zip) generates the maximum of values in "welfare_levels". Really simple code to do this that I've made is below. However when I try to solve this it generates a list index out of range error. What is the issue with trying to index the list using the max_welfare_index?

tau_grid = np.linspace(0.0001, 1-1e-5,15)
tau_b_grid = np.linspace(0.0001, 1-1e-5,15)

welfare_levels = []

tax_zip = zip(tau_grid, tau_b_grid)

for t, tb in tax_zip:
    S = society_welfare (0.6, t, tb)     
    welfare_levels.append(S)

maximum_welfare = max(welfare_levels)
max_welfare_index = welfare_levels.index(maximum_welfare)
print(max_welfare_index)

tax_zip2= list(tax_zip)

maximizing_taxes = tax_zip2[max_welfare_index]
print(maximizing_taxes) 
1 Answers

Here's the problem you have. zip produces an iterator. After you finish the for loop, the iterator is finished. So, tax_zip2 receives an empty list. You would have learned that if you had printed out intermediate values.

Fix this by converting to a list early on:

tax_zip = list(zip(tau_grid, tau_b_grid))

Now you can reuse tax_zip several times.

Related