ST EK List:
No EK's found

Keeping Items from a List

On this page, you will use predicates with the keep block to keep items in a list that have specific characteristics.

As you know, predicates can be used with conditionals to decide when to do something; they can also be used with keep to decide which things to keep. The keep items such that () from () block takes a list and a predicate as input, and it reports a new list keeping only the items from the list that make the condition described by the predicate true.

For example, the following expression will find words from the words list whose first letter is v. The blank input slot is where each item of the list goes to decide if it will be kept.
keep items such that ((letter(1) of ( )) = (v)) from (words list)

You write the predicate that does the checking, and keep applies the predicate to each item in the input list and then reports the list of items that make your predicate true.

Note that the blank input slot in the predicate is required when using keep. This is where the item from the list goes each time the question is asked.

  1. Click here to load this file. Then save it to your Snap! account.
    Export your new predicates (between?, divisible?, even?, and perhaps also integer?) from your project in Unit 2 Lab 3 Page 1: What is a Predicate? and then import them into the new project and save the new project. To review how to import and export blocks you may wish to revisit Unit 2 Lab 1 Page 4: Importing Greet Player into Another Program .
  2. Talk with Your Partner Experiment with these examples of keep. Discuss and then explain in writing what these expressions are doing.
    keep items such that ((length of ( )) = (5)) from {apple, banana, orange, grape, kiwi, mango, watermelon} reporting {apple, grape, mango}
    keep items such that (not(is () a (number)?)) from {5, :), six, 7, elephant, 3} reporting {5,7,3}
  3. Import Tools Like the for block you have seen in Unit 1 Lab 3 Page 6: Looping with a Counter, keep isn't installed automatically. It has already been added to this project, but in other projects, you'll need to select "Import tools" from the File menu to access the keep block.

The first example keeps inputs of a certain length; every word in the list is asked whether its length is 5, and only those with five letters are reported. The second example keeps inputs that are not numbers; every item in this list is asked "Is this item not a number?" and only the elements that are not numbers are reported.

  1. Run initialize lists, and then use your new predicates together with keep to answer these questions:
    1. How many 12-letter words are in words list?
    2. How many 15-letter words are in words list?
    3. How many 17-letter words are in words list?
    4. How many items in the numbers list are:
      1. Even?
      2. Between 25 and 75?
      3. Even numbers that are greater than zero?
  1. If you created an integer? predicate, use your new predicates together with keep to determine how many numbers in the numbers list are:
    1. Integers?
    2. Integers between 25 and 75?
    3. Not integers?
    4. Odd integers?