Reputation: 35
I keep getting the error " TypeError: list indices must be integers or slices, not list" and im not sure on how to fix it what do i need to change?
@app.route("/browse/<where>")
def collectPage(where):
for item in lostItems:
if item[1] == where:
passedItem = lostItems[item]
return render_template("mainPage.html", collect = Collect, item = passedItem)
Upvotes: 1
Views: 357
Reputation: 1115
It looks like lostItems
is a nested list. and when you use lostItems[item]
you are getting the error. you can change your statement to passedItem = item
.
@app.route("/browse/<where>")
def collectPage(where):
for item in lostItems:
if item[1] == where:
passedItem = item
return render_template("mainPage.html", collect = Collect, item = passedItem)
or you can use enumetrate
to access the list index.
@app.route("/browse/<where>")
def collectPage(where):
for indx,item in enumerate(lostItems):
if item[1] == where:
passedItem = lostItems[indx]
return render_template("mainPage.html", collect = Collect, item = passedItem)
Upvotes: 0
Reputation: 7268
Try:
def collectPage(where):
for item in range(len(lostItems)):
if item[1] == where:
passedItem = lostItems[item]
return render_template("mainPage.html", collect = Collect, item = passedItem)
Or
def collectPage(where):
for item in lostItems:
if item[1] == where:
passedItem = item
return render_template("mainPage.html", collect = Collect, item = passedItem)
Upvotes: 1