You might want to have two different functions, one that returns all sub-lists that contain the search element, and one just returns the first sub-list that has the search element if it exists:
def get_all_lists_with_element(list_of_lists: list[list[str]],
element: str) -> list[list[str]]:
"""Returns all sub-lists that contain element"""
return [xs for xs in list_of_lists if element in xs]
def get_first_list_with_element(list_of_lists: list[list[str]],
element: str) -> list[str]:
"""Returns first sub-list that contains element"""
return next((xs for xs in list_of_lists if element in xs), None)
list_of_lists = [
['ID', 'Last', 'First', 'GradYear', 'GradTerm', 'DegreeProgram'],
['101010', 'Lee', 'Shane', '2019', 'Spring', 'MSA'],
['101010'],
]
input_str = input('Enter string to search for in list of lists: ')
all_lists_with_input = get_all_lists_with_element(list_of_lists, input_str)
print(f'all_lists_with_input={all_lists_with_input}')
first_list_with_input = get_first_list_with_element(list_of_lists, input_str)
print(f'first_list_with_input={first_list_with_input}')
Example usage with input that exists:
Enter string to search for in list of lists: 101010
all_lists_with_input=[['101010', 'Lee', 'Shane', '2019', 'Spring', 'MSA'], ['101010']]
first_list_with_input=['101010', 'Lee', 'Shane', '2019', 'Spring', 'MSA']
Example usage with input that does not exist:
Enter string to search for in list of lists: abc
all_lists_with_input=[]
first_list_with_input=None