Expanding on DainDwarf's answer, this will return multiple values if the searched item is in more than one of the nested lists:
def find_in_list_of_list(mylist, char):
found_list = []
for sub_list in mylist:
if char in sub_list:
found_list.append(f'({mylist.index(sub_list)}, {sub_list.index(char)})')
if len(found_list) > 0:
return found_list
example_list = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h'], ['a', 'b', 'c', 'd']]
print(find_in_list_of_list(example_list, 'c'))
I also got en error when changing the search from 'b' to 'x' in Dwarf's code:
Traceback (most recent call last):
File "C:\main.py", line 9, in <module>
print(find_in_list_of_list(example_list, 'x'))
File "C:\main.py", line 5, in find_in_list_of_list
raise ValueError("'{char}' is not in list".format(char = char))
ValueError: 'x' is not in list
a
– Petrochemicalnumpy
tag? Your expression usingnp.where
does not make sense. – Archfiend