Hongteng Wang
Hongteng Wang

Reputation: 33

search an element and return the list within list of lists

I have the following list

list_of_lists= [['ID', 'Last', 'First', 'GradYear', 'GradTerm', 'DegreeProgram'], ['101010', 'Lee', 'Shane', '2019', 'Spring', 'MSA'], ['101020', 'Zhang', 'Eve', '2019', 'Summer', 'MSSD'], ['101030', 'Anthony', 'Daisy', '2020', 'Fall', 'MSBA']]

Now, when I have an input of e.g. 10, I'd like to retrieve 'all list which contains ID starts with 10.

Upvotes: 1

Views: 969

Answers (6)

Tzane
Tzane

Reputation: 3462

filter also works, especially if you want to find all matches:

list_of_lists= [
    ['ID', 'Last', 'First', 'GradYear', 'GradTerm', 'DegreeProgram'],
    ['101010', 'Lee', 'Shane', '2019', 'Spring', 'MSA']
]
find = '101010'
res = list(filter(lambda x: find in x, list_of_lists))
print(res)

Upvotes: 0

Abhijit Sarkar
Abhijit Sarkar

Reputation: 24518

ll = [
    ['ID', 'Last', 'First', 'GradYear', 'GradTerm', 'DegreeProgram'],
    ['101010', 'Lee', 'Shane', '2019', 'Spring', 'MSA']
]

y = next((x for x in ll if '101010' in x), [])

['101010', 'Lee', 'Shane', '2019', 'Spring', 'MSA']

Upvotes: 0

Sash Sinha
Sash Sinha

Reputation: 22275

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

Upvotes: 1

fam-woodpecker
fam-woodpecker

Reputation: 707

You will need to search the internal lists not the list of lists as a whole.

input = '101010'

for lst in list_of_lists:
    if input in lst:
        # Do what you want with the list containing the input

Upvotes: 1

Jan
Jan

Reputation: 43169

You may simply use in:

list_of_lists= [
    ['ID', 'Last', 'First', 'GradYear', 'GradTerm', 'DegreeProgram'],
    ['101010', 'Lee', 'Shane', '2019', 'Spring', 'MSA']
]

needle = '101010'
result = [lst for lst in list_of_lists if needle in lst]
print(result)

See a demo on ideone.com.

Upvotes: 1

Usama Tariq
Usama Tariq

Reputation: 179

This might help you

def complete_list(input, list_of_lists):
    for i in list_of_lists:
        if input in i:
            return i
    return None

Feel free to ask if you have any question

Upvotes: 0

Related Questions