0
how do u do this ........The index method finds the first occurrence of a list item and returns its index. If the item isn't in
......
2 Réponses
+ 12
list = ['spam','egg']
print(list.index('egg')) # 1
print(list.index('spam')) # 0
# if searched item is present more than once, the 'index' method return only the lowest index:
list = ['spam','egg','spam','spam']
print(list.index('spam')) # 0
# if searched item not present, 'index' method throw an error:
print(list.index('bacon')) # Traceback (most recent call last): File "<path><filename>", line <linenumber>, in <module> ValueError: 'bacon' is not in list
# use 'enumerate' function allow searching for all items in list:
list = ['spam','egg','spam','spam']
for index, value in enumerate(list):
if value == 'spam':
print(index,value)
# output:
"""
0 spam
2 spam
3 spam
"""
- 1
lol