0

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!

2
  • what have you tried so far? Commented Jan 13, 2023 at 17:46
  • @JonSG too many attempts to list, but each web search query returned a response of "filtering dictionary." Primarily, I tried to equate each dict key to the corresponding index, but was unable to find an effective solution that iterated through the dictionary for a given element. Commented Jan 13, 2023 at 19:32

1 Answer 1

2

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]
]
Sign up to request clarification or add additional context in comments.

2 Comments

with a slight modification to subl[k] to subl[k-1] to align the literal dictionary key to the list index, it looks like this works. Thank you!
@paaskanama Yep, subl[k-1], I've pasted the wrong code :) Fixed.

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.