Printing full list instead iterable list within a if loop

Viewed 52

I'm creating a class search program that accepts a keyword and searches it through the description elements of a class.

Im trying to get result[[]] to return the whole list(r) but it only returns the rest of list(r) after the search term. I don't understand why r would change because of the if statement.

table_row_col is a 2d array in [class][description elements] format and search term is a string.

def search_class(search_term, table_row_col):
    result = [[]]
    row = 0
    for r in table_row_col:
        for c in r:
            if search_term in c:
                result.insert(row, list(r))
                row += 1
    return result

Example result: Keyword 'Poly' result = [In-Person TR 01:40 PM 02:55 PM WNS 506 Schmitt, J]

Rather then

[MATH 09001 Polynomial equations In-Person TR 01:40 PM 02:55 PM WNS 506 Schmitt, J]

When I put the result.insert statement outside the if loop then the whole class description is printed

Any help would be greatly appreciated. It's my first time using python and I'm still adjusting to the for loops.

1 Answers

Something like this should work. I've changed it a bit, but nothing here is too crazy.

def search_class(search_term, table_row_col):
    result = []
    for row in table_row_col:
        if any(search_term in c for c in row):
            result.append(row)
    return result

table_row_col = [["MATH", "09023", "Polynomial equations", "In-person", "TR", "1:40PM", "506 Schmitt JR"],
                 ["MATH", "09023", "Calculus", "Blended", "TR", "1:50pm", "704 Gregg]"],
                 ["MATH", "09023", "Pressing the keys on a Calculator", "Virtual", "TR", "8:50pm", "804 JimmyCarlos]"]]

print(search_class("Calc",table_row_col))

Let me know any questions you have. If you're actually timetabling real lessons - hope it goes well! That can be a pain to do without a computer!

Related