Coding for KidsCoding for Kids
Creative LevelsChallengesTeacher's Guide
Vote for features
Advanced Python Development Course
Chapter
>
Level

Advanced Lists
List Comprehensions

In this chapter we’re going to explore new ways for using lists in Python, regarding optimization, compartmentalization and even making large multi-dimensional lists like Matrixes that closely resemble tables.

For this level we will be working with List Comprehensions, these are lists that can be created from other lists by only selecting certain components from them. This is also done in a very straightforward single line of code without having to manually input a list or come up with a complex automations.

Guide

Objective

Grab and sort all the root vegetables in the pantry so you may store them in their correct place using list comprehensions.

The pantry has several sacks full of root vegetables that need to be sorted, these being: potatoes , onions and garlic, they likely have veggies that have gone bad. Take out the vegetables that are good and store them in the crates where they belong.

In order to accomplish this you need to identify what items are inside each sack, there are three (3) list constants that hold the information of each one of the sacks, these are named: left_sack , middle_sack , right_sack . These list constants correspond to sacks on the field in the locations outlined by their names.

Walk to the light X marks in front of each sack and use the speak() function to identify the contents of each sack by using the constants, for example: player.speak(left_sack). Once you identify the contents inside each sack identify items that need to be removed such as vegetables with: "Bad" , "Spoiled" or "Rotten" in their name.

You need to create new lists that only contain items that are good for consumption, you could do this manually but it would take a while and it could get rather confusing. To accomplish this more effectively you can create a list out of another list, this is known as a List Comprehension.

From the three (3) list constants: left_sack , middle_sack , right_sack create three new lists named: potatoes , onions and garlic that do not contain undesired items in the sacks. List Comprehensions allow you to create new lists without writing a whole bunch of code, for example:

# Original list sack = ["Potatoes", "Sweet Potatoes", "Spoiled Potatoes" ] # List Comprehension potatoes = [x for x in sack if not "Spoiled" in x] # This will create a list named potatoes with: # ["Potatoes", "Sweet Potatoes"]

In the above example, the list named potatoes is created out of the list named sack only containing ["Potatoes", "Sweet Potatoes"] and excluding "Spoiled Potatoes". This is achieved by creating a self contained for loop, in this case using x as an index to go through the list and for / in statements to identify and exclude the undesired element, that being any string that has the word "Spoiled".

To review, find out if the list constants have the word "Bad" , "Spoiled" or "Rotten" in them using the speak() function, create lists potatoes , onions and garlic using List Comprehensions removing the undesired elements you found in each list.

Once these lists are created, walk to each of the three (3) gold X marks in front of the crates with the vegetable signs, and place the lists, using the place() function, in the corresponding crates in order to complete the level. Locations are outlined by the signs, those being onions on the left crate, potatoes in the middle crate and garlic on the left crate.

Code book