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.

2
  • 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? Commented Nov 9, 2021 at 5:52
  • Does this answer your question? "Least Astonishment" and the Mutable Default Argument Commented Nov 9, 2021 at 5:54

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

You are correct on both, just wanted it to be clear that the iterating item was a list
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

Comments

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

3 Comments

it returns None
list_of_lists= [['ID', 'Last', 'First', 'GradYear', 'GradTerm', 'DegreeProgram'], ['101010', 'Lee', 'Shane', '2019', 'Spring', 'MSA']] inp=101010 def complete_list(inp, list_of_lists): for i in list_of_lists: if input in i: return i return None
It is returning me a correct answer
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']

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.