Ep. 3 Exercise: Lists in Python


Now that we’re used to storing individual items like Spanish words and ingredients, it’s time to introduce lists—the perfect way to keep all of these items in one organized place. Think of a list like a grocery basket where you can store everything you need for your recipe.

Lists in Simple Terms:

  • A list is a container that can hold multiple items, like "tomato", "onion", and "garlic".
  • Lists are created using square brackets: shopping_list = ["tomato", "onion", "garlic"].

In Episode 2, we stored several Spanish words and ingredients, but each one needed its own variable. Lists help us tidy that up by putting related items together in a single place.

Using lists means we don’t have to create a new variable for every new word or ingredient. Instead, we use one list to store many things. This makes it a lot easier to manage when we have lots of words or ingredients to keep track of.

Think of a list like a single bag where you can toss in all the groceries you need for a meal. Instead of carrying each vegetable separately, you have them all together!


Indexing in Lists

Lists in Python are ordered, meaning each item has a specific position called an index. Indexes are used to access items inside a list, and they start at 0.

How Indexing Works:

  • Imagine your shopping basket as a line of items. The first item is at position 0, the next at 1, and so on.
  • If you have shopping_list = ["tomato", "onion", "garlic"], "tomato" is at index 0, "onion" at index 1, and "garlic" at index 2.

Accessing Items by Index:

  • To get an item from the list, we use its index in square brackets.
  shopping_list = ["tomato", "onion", "garlic"]
  print(shopping_list[0])  # Outputs: tomato
  print(shopping_list[2])  # Outputs: garlic

Changing Items Using Index:

  • We can also change an item in the list by using its index.
  shopping_list[1] = "pepper"
  print(shopping_list)  # Outputs: ['tomato', 'pepper', 'garlic']

This replaces "onion" with "pepper".

Negative Indexes:

  • Python allows us to use negative indexes to start counting from the end of the list.
  print(shopping_list[-1])  # Outputs: garlic (last item)

Guided Practice

  • Create a list called favorite_words with at least three Spanish words.
  • Print the word at index 1.
  • Replace the word at index 2 with a new word.
  • Try accessing the last item using a negative index.

Operations

  • Creating a List:
  shopping_list = ["tomato", "onion", "garlic"]

This creates a list with three items.

  • Adding an Item to the List:
  shopping_list.append("milk")

"milk" is now added to the end of our list.

  • Removing an Item from the List:
  shopping_list.remove("onion")

"onion" has been removed from the list.

  • Checking the Number of Items:
  print(len(shopping_list))

This will print the number of items in the shopping_list.

  • Accessing Items by Index:
  print(shopping_list[0])  # Outputs: tomato

Real-World Application:
Imagine making a list of friends to invite to a party. You write down their names on a single piece of paper—that’s a list. You add new names when you think of more friends and cross out names if they can’t make it.


Built-In Functions and Methods

  • .append(): Adds an item to a list. For example:
  spanish_words = ["hola", "gracias"]
  spanish_words.append("adiós")

Now, "adiós" has been added to our list of words.

  • .remove(): Removes an item from a list:
  spanish_words.remove("gracias")

"gracias" is removed from the list.

  • len(): Tells us how many items are in the list:
  print(len(spanish_words))  # Outputs the number of words in the list
  • Index Access:
  print(spanish_words[1])  # Outputs the second item in the list: 'gracias'

Guided Practice

  • Create a list named favorite_words and add at least three Spanish words to it.
  • Print the word at index 1.
  • Remove one word and then print out how many words are left using len().
  • Access the last word using a negative index.

What’s Needed for Basil

For Basil, we need it to handle shopping lists and vocabulary lists more efficiently. Instead of creating a new variable for each ingredient or Spanish word, we’ll use lists to store everything in one place. This makes Basil much more useful and tidy!


Basil Code Implementation

  1. Create a Shopping List in ingredients.py:
   shopping_list = ["tomato", "onion", "garlic"]
  1. Add More Ingredients:
  • If we realize we need more, like "milk":
   shopping_list.append("milk")
  1. Remove an Ingredient:
  • If we decide we don’t need "onion" anymore:
   shopping_list.remove("onion")
  1. Access an Ingredient by Index:
  • To see what’s at the first position:
   print(shopping_list[0])  # Outputs: tomato
  1. Print the Shopping List:
  • In main.py, let’s print out what’s in our list:
   from ingredients import shopping_list

   print("Shopping List:", shopping_list)
   print("Total items:", len(shopping_list))


Episode 4 brings us Python dictionaries.

Scroll to Top