Will Layton
Will Layton

Reputation: 35

TypeError: list indices must be integers or slices, not list - What do i do?

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

Answers (2)

Prudhvi
Prudhvi

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

Harsha Biyani
Harsha Biyani

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

Related Questions