Lists Practice Checklist

See the notes for Python Lists.

1) Create a list of numbers and print the list.
Example Output:

[3, 2, 9, 7, 7]

2) Create a list containing three strings (text) and print the list.
Example output:

['soap', 'deodorant', 'toothpaste']

3) Using the starter code below, print item number 0 and item number 2 from the list on separate lines.
Starter Code:

fruits = ['apple', 'orange', 'sand', 'gravy']

Expected Output:

apple
sand

(Don’t do “print(‘apple’)” in your code! Replace ‘apple’ with the name of the list + square brackets to ndeget one item)

4) Using the starter code below, add some more interesting choices to the list and print the modified list. (hints: += and remember [ ], or use append() function )
Starter Code:

choices = ['rock', 'paper', 'scissors']
#add code here
print(choices)

Sample Output Code:

['rock', 'paper', 'scissors', 'lizard', 'Spock']

5) Change the starter code so that every sentence says a different favorite color. Use list indexing and square brackets. (Yes, you can also do this faster with a loop, but this activity is designed for people who probably don’t know how to do loops yet)
Starter Code:

colors = ['red','blue','pink','black']
print('My new favorite color is', colors, '.')
print('My new favorite color is', colors, '.')
print('My new favorite color is', colors, '.')
print('My new favorite color is', colors, '.')

Example output:

My new favorite color is red .
My new favorite color is blue .
My new favorite color is pink .
My new favorite color is black .

(Bonus: fix the code so it won’t put a space before each period.)

6a) Print the length of this list:
Starter Code:

scores = [1, 2, 1.5, 2, 4, 4, 3.5, 4, 3]
print(scores)

Output: 9

6b) Slice the scores list and print it so it only prints the first four numbers in the list. Output should look like this:

[1, 2, 1.5, 2]

6c) Slice the scores list from the previous part so it only prints the last five values.
Output:

[4, 4, 3.5, 4, 3]

7) Ask the user to provide a number, then print that item number from the list. (Hint: convert the answer to int)
Starter Code:

items = ['string', 'rock', 'candy', 'ball', 'house']
a = input('Which item do you want?')

Example Output:

Which item do you want? 1
rock

8) Create a variable that contains a blank list []. Then begin a for loop that asks a user to name an item five times. Each time, add this item to the list. Print the list at the end.
Example Output:

Name a thing: lawnmower
Name a thing: snowblower
Name a thing: spatula
Name a thing: oven
Name a thing: chair
Here is your list of things:
['lawnmower', 'snowblower', 'spatula', 'oven', 'chair']

Starter Code with the for loop provided:

#indent your repeating code after this for loop
for i in range(5):


9) Add a line of code to remove “grass” from the list. (hint: list.remove())
Starter Code:

foods = ['bread', 'pasta', 'grass', 'eggs', 'pie']
#add code here
print('Humans can eat these foods:')
print(foods)

10) Add a line of code to create a new list named humanfoods that includes only the items that humans eat. (Hint: slice the foods list twice and add the slices together)
Starter Code:

foods = ['bread', 'pasta', 'grass', 'eggs', 'pie']
#create new list here
print('Humans can eat these foods:')
print(humanfoods)

11) Ask the user to name one of the suits in a deck of playing cards. Tell them if they named one correctly or not. (hint: use “in”)
Starter Code:

suits = ['hearts','spades','clubs','diamonds']

Example Output:

#correct response example:
Name a suit in a deck of cards: spades
Good job. spades is a suit.

#incorrect response example:
Name a suit in a deck of cards: potatoes
Sorry, potatoes is not a suit.

12) Use the split() function to make a list of words with the starter code below.

text = "this string contains several spaces"
# finish the next line of code
words = 

#These should each print one word:
print(words[0])
print(words[1])

13) Fix these lists so the animals aren’t mixed up with the plants. Use the remove() and append() functions, then print the fixed lists at the end.

animals = ['cat', 'tree', 'dog', 'grass']
plants = ['flower', 'fern', 'frog', 'bird', 'shrub']

14) Use pop() and del to remove items by its number position within the list.

Using the starter code, “pop” item number 1 and then “del” item number 3. Print the new, shorter list.

stuff = ['egg','cat','box','mat','emu']

15) Print a randomly chosen food item from the list.

foods = ['sushi', 'taco', 'sandwich', 'eggs']

See the random module notes.

You can do this in a couple of different ways:
1) Use randint() function to get a random integer, then look up that item number.
2) Use the choice() function to retrieve a random item.

You should try it both ways to see the difference.