def print_index_of_item(lst, item): \"\"\"Print the index of an item in a list,
ID: 3848132 • Letter: D
Question
def print_index_of_item(lst, item):
"""Print the index of an item in a list, followed by the item.
If the item appears multiple times in the list, return the index and
the item for each time it appears in the list.
If the item does not appear, print '_item_ not found' (substitute the
name of the item.)
>>> print_index_of_item(['a', 'b', 'c', 'b', 'c'], 'b')
1 b
3 b
>>> print_index_of_item([1, 2, 3], 4)
4 not found
>>> print_index_of_item(['apple', 'berry', 'cherry', 'durian'], 'apple')
0 apple
"""
Explanation / Answer
def print_index_of_item(lst, item):
flag=0
i=0
for lst_item in lst:
if(item==lst_item):
print(' found item : ',item,' at index : ',i)
flag=1
i=i+1
if(flag==0):
print('item not found : ',item)
print_index_of_item(['a', 'b', 'c', 'b', 'c'], 'b')
print_index_of_item([1, 2, 3], 4)
print_index_of_item(['apple', 'berry', 'cherry', 'durian'], 'apple')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.