List Filter Pattern

Filtering one list

This pattern allows the user to filter a list into a new list by limiting the information that is added to the new list. For example, you could filter a list of words so that only words of a certain length or words that start with a specific letter appear in the new list.

Name Code
List Filter - starting letter (block)
List Filter - starting letter (text)
Name Code
List Filter - length (block)
List Filter - length (text)

How does it work?

  • Create a variable and store a list in it
  • Create a blank list to store the filtered list
  • In a function use a for loop to access each element in the original list
  • If that element meets a certain condition, append it to the filtered list

Filtering multiple lists

What if you have multiple lists, but want to filter all the lists based on the information in a single list?

Name Code
Multiple List Filter (block)
Multiple List Filter (text)
  • All the lists must be the same length for this pattern - ideally they are columns pulled from a table

How does it work?

  • Create a variable for each list
  • Create blank lists to store the filtered lists
  • In a function, first reset all filtered lists to blank lists. Every time the function is called, the filtered lists will reset.
  • Use a for loop to access each item in the list that contains the element you are filtering by
  • If the element is found, append to each filtered list the element at the index in the original lists where the element was found

Found a bug in the documentation? Let us know at documentation@code.org