list.index() not quite working

python

Solution

`apple` is not in `smallist`. It is in a nested list contained inside of `smallist`.

You'll have to search for it with a loop:

for i, nested in enumerate(smallist):
    if item in nested:
        print(i)
        break

Here `enumerate()` creates a running index for us while looping over `smallist` so we can print the index where it was found.

If what you wanted to do was print the other value we don't need the index:

for name, count in smallist:
    if name == item:
        print(count)
        break

But it'd be easier to use a dictionary here:

small_dict = dict(smallist)
print(small_dict.get(item, 'Not found'))

Problem

I'm using this piece of code for a small program I need to design for a friend. The problem is can't quite get it to work. I am designing a program that uses list for vegetables and fruits. For example my list is: ``` smallist = [["apple", 2], ["banana", 3], ["strawberry",1]] item = input("Please give the name of the fruit\n\n") smallist.index(item) print (smallist) ``` The problem is when I then try to find the index of lets say the apple. I just say that the apple does not exist. ``` smallist.index(item) ValueError: 'apple' is not in list ``` I can't figure out why it won't show me the apple with its value which in this case would be 2

Original source