Filter a Python list using Dictionary keys and values

Question:

GOAL: Filter a list of lists using dictionary as reference in Python 3.8+

CASE USE: When reviewing a nested list — a series of survey responses — filtering out responses based on control questions. In the dictionary, the responses to questions 3 (index 2 in list) and 7 (index 6) should both be of corresponding value 5. If both answers for a response are not 5, they should not be populated in the filtered_responses list.

Open to interpretation on how to solve for this. I have reviewed several resources touching on filtering dictionaries using lists. This method is preferred as some survey responses many contain the same array of values, therefore the list element is retained.

no_of_survey_questions = 10
no_of_participants = 5
min_score = 1
max_score = 10

control_questions = {3: 5,
                     7: 5, }

unfiltered_responses = [[4, 5, 4, 5, 4, 5, 4, 5, 4, 5],  # omit
                        [9, 8, 7, 6, 5, 4, 3, 2, 1, 1],  # omit
                        [5, 5, 5, 5, 5, 5, 5, 5, 5, 5],  # include
                        [5, 2, 5, 2, 5, 2, 5, 9, 1, 1],  # include
                        [1, 2, 5, 1, 2, 1, 2, 1, 2, 1]]  # omit

for response in unfiltered_responses:
    print(response)

print()

filtered_responses = []  # should contain only unfiltered_responses values marked 'include'
for response in filtered_responses:
    # INSERT CODE HERE
    print(response)

Thanks in advance!

Asked By: paaskanama

||

Answers:

You can use list comprehension + all():

control_questions = {3: 5,
                     7: 5}

unfiltered_responses = [[4, 5, 4, 5, 4, 5, 4, 5, 4, 5],  # omit
                        [9, 8, 7, 6, 5, 4, 3, 2, 1, 1],  # omit
                        [5, 5, 5, 5, 5, 5, 5, 5, 5, 5],  # include
                        [5, 2, 5, 2, 5, 2, 5, 9, 1, 1],  # include
                        [1, 2, 5, 1, 2, 1, 2, 1, 2, 1]]  # omit

filted_questions = [subl  for subl in unfiltered_responses if all(subl[k-1] == v for k, v in control_questions.items())]
print(filted_questions)

Prints:

[
   [5, 5, 5, 5, 5, 5, 5, 5, 5, 5], 
   [5, 2, 5, 2, 5, 2, 5, 9, 1, 1]
]
Answered By: Andrej Kesely