Managing a List of Lists in Python

list of lists

A Lesson in Pizza…

In this short lesson, we’re going to use Python to get user input from a customer at our pizzeria. Someone I know told me the pizza analogy has been done to death, so I switched this one up a bit.

In this example, we’ll ask the customer what they want on each half of their pizza, and then combine it all together into a single list.

Let’s go!

Setting Up

First thing’s first. We need to generate our prompts for the customer. There are a few ways to do this, but I prefer to hold the prompt in a variable so I can simply call the variable anytime I want to use the prompt.

prompt_left = "Enter up to 5 toppings for the left side of your pizza."
prompt_left += "\nType 'done' to move on to the second half: "
prompt_right = "Enter up to 5 toppings for the right side of your pizza."
prompt_right += "\nType 'done' to finish your pizza: "

In this example, I want my prompts to display on two separate lines. With Python, this is simple. In line 1, prompt_left holds the first line. In line two, I add the second line with the += operator. Normally this would concatenate the two strings into one long, but I used \n (new line) at the beginning of the second string.

Note: You can do this all on a single line, but by doing so your code will exceed 79 characters in length. See the PEP-8 guidelines for more details.

Making Our Lists

Since the pizza will have two halves, we will store the toppings in two lists. Since we don’t know what the customer wants, these will be empty.

pie_left = []
pie_right = []

Now, let’s store the halves of the pizza in a new list called pizza. This will make our pizza whole.

pizza = [
    pie_left,
    pie_right
]

In Python, you can store lists in lists. You can even store dictionaries in lists. It’s very flexible.

Populating the Lists In a While Loop

At this pizzeria, there are a maximum of 5 toppings allowed on each side. We’re going to create a counter in each loop so we know when we’ve reach 5 the limit.

Initializing a Counter

To start, we’re going to hold an integer in a variable that we’ll call topping_count. We’ll be able to use it for each side.

topping_count = int(0)

Building the While Loop

Now that we have the foundation for our counter, we can build a while loop. Here’s what it looks like:

while topping_count < 5:
    topping_left = input(prompt_left)
    if topping_left == 'done':
        break
    elif topping_left == 'exit':
        exit()
    else:
        pie_left.append(topping_left)
        topping_count += 1

Let’s take a closer look at what we’re doing here. The while loop will execute until a bool value changes. In this instance, topping_count will be False until it reaches 5.

Now, we can finally prompt for user input. Remember, we made a variable called prompt_left at the very beginning. topping_left = input(prompt_left) will print the string held by prompt_left, capture the customer’s input via, well, input, and store it in topping_left.

Using Break

There is a way to stop a while loop when a bool doesn’t change, and it’s simple. We’ll put an if statement inside the while loop so we can react based on the customer’s input.

    if topping_left == 'done' :
        break

What if the customer only wants two toppings on the left half? This gives our customer a way to exit the loop. In this instance, a user input of done will break the while loop and move on to the next bit of code.

Exiting

Let’s give the customer a way to exit the program, since an input of done will simply move on to the next part of the program.

    elif topping_left == 'exit':
        exit()

Adding the Ingredients

Remember the list we created? pie_left is still empty, but we can add items to it using the append method. Since we held the customer’s input in topping_left, we can read that string and add it the the list.

    else:
        pie_left.append(topping_left)

Remember, the while loop will keep going until the bool (True, False) changes. We’re only allowed to have 5 ingredients on each half, so each time an ingredient is added to the list we add 1 to topping_count.

        topping_count += 1

Once topping_count equals 4, Python will exit the loop and move on. Why 4? In most programming languages, base-10 counting starts at 0.

Moving to the Next While Loop

The second loop is identical to the first, but it won’t work because our counter is full. We can get around that by initializing it.

topping_count = 0

Do you notice anything unusual about this? Before, we gave topping_count a value of int(0). Otherwise, it would have held 0 as string. As a string, we can’t do math. However, since the value is already an integer, I only have to do topping_count = 0.

This effectively resets the counter, allowing the next loop to function like the last.

while topping_count < 5:
    topping_right = input(prompt_right)
    if topping_right == 'done' :
        break
    elif topping_right == 'exit':
        exit()
    else:
        pie_right.append(topping_right)
        topping_count += 1

Did it Work?

To test, just do:

print(pizza)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.