1

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.

  • Are you trying to retrieve the lists where the ID (in this case, I'm assuming the values at position 0) matches your input? Or is the index of the matching-input-value not important? – Walker Sutton Nov 09 '21 at 05:52
  • Does this answer your question? ["Least Astonishment" and the Mutable Default Argument](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument) – Gray Nov 09 '21 at 05:54

6 Answers6

1

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
fam-woodpecker
  • 500
  • 2
  • 10
1

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.

Jan
  • 42,290
  • 8
  • 54
  • 79
1

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
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
0

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

Usama Tariq
  • 169
  • 5
0
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']

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
0

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)
Tzane
  • 2,752
  • 1
  • 10
  • 21