def search_linear(value, list): idx = 0 found = False while not found and idx < len(list): if list[idx] == value: found = True else: idx += 1 return idx if idx < len(list) else None list = [1,3,9,2,0,6,4,7,5,3] idx = search_linear(5, list) print(idx) idx = search_linear(15, list) print(idx)
tel_list = [ {'number': 123123123, 'name': 'photovoltaic'}, {'number': 789789789, 'name': 'subscription of magazines'}, {'number': 567567567, 'name': 'lottery'}, {'number': 345345345, 'name': 'show of pots'}, {'number': 678678678, 'name': 'grandson asking for money'}, {'number': 234234234, 'name': 'loans'}, {'number': 456456456, 'name': 'free medical examinations'} ] def search_linear(number, list_of_dicts): idx = 0 found = False while not found and idx < len(list_of_dicts): if list_of_dicts[idx]['number'] == number: found = True else: idx += 1 return idx if idx < len(list_of_dicts) else None number = 567567567 idx = search_linear(number, tel_list) print(f'The phone number {number} details are under index: {idx}') if idx: print(tel_list[idx])