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

......

20th Mar 2017, 3:21 PM
unjel masters
unjel masters - avatar
2 Respuestas
+ 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 """
20th Mar 2017, 8:58 PM
visph
visph - avatar
- 1
lol
27th Mar 2017, 1:39 PM
GotEmmm