'How do I find an item in my list without using indexes [closed]
How do I find an item in my list without using indexes.
So i have a list and I want to find the item in a list.
Solution 1:[1]
Are you simply checking to see if an item exists within the list?
If so user the in operator Example:
# Example
my_list = [4,5,7,2,4,5]
if 7 in my_list:
print("Item found")
Or if you are trying to return the index of the item found do this:
my_list.index(7)
# returns 2
Solution 2:[2]
If you have the list i would use this:
fruits = ['banana', 'apple', 'watermelon', 'peach']
index = fruits.index('apple')
print('The index of apple is:', index)
OUTPUT: 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alex Joslin |
| Solution 2 | Snir Ego |
