Overview
In this lesson, you will learn about lists in Python. This concept will help you unlock further self-driving capabilities of your car.
This lesson prepares you for future activities like line following, where your car must make decisions based on what its sensors detect. You can follow along using the lists_lesson.ipynb notebook in the units/7.line_follower folder.
Introducing Lists in Python
When the grayscale sensor is read, it doesn’t return just one value — it returns three values at the same time. Python groups these three values together using a structure called a list.
A list is:
- A container that holds multiple values
- Ordered (each value has a specific position)
- Stored using square brackets
Example of what a grayscale reading might look like:
[312, 540, 278]
This is a list with three numbers. Each number represents one sensor.
Order matters:
- First number → Left sensor
- Second number → Middle sensor
- Third number → Right sensor
Understanding List Positions
In Python, each value in a list has an index (position). It is important to remember that the first position starts at 0, not 1, in Python. So, for instance, your grayscale module data might look similar to the table below:
| VALUES | First Value | Second Value | Third Value |
|---|---|---|---|
| INDEX | 0 | 1 | 2 |
| VALUES | 312 | 540 | 278 |
| POSITIONS | Left | Middle | Right |
This means:
values[0]= Left sensorvalues[1]= Middle sensorvalues[2]= Right sensor
You don’t need to memorize this yet. Just understand that each sensor reading has a specific position in the list.
Accessing Individual Values From a List
Sometimes you do not want the whole list. You only want a single value, such as the left sensor or the middle sensor.
There are two common ways to do this in Python.
Using Square Brackets
You can grab one value at a time using the list name and an index inside square brackets.
values = px.get_grayscale_data() # something like [312, 540, 278]
left_value = values[0]
middle_value = values[1]
right_value = values[2]
print("Left:", left_value)
print("Middle:", middle_value)
print("Right:", right_value)
Here you can clearly see which sensor reading is being used in each line.
Unpacking Into Separate Variables
Python also lets you split a list into separate variables in one line. This is called unpacking.
values = px.get_grayscale_data()
left_value, middle_value, right_value = values
print("Left:", left_value)
print("Middle:", middle_value)
print("Right:", right_value)
This does the same thing as the previous example, but it can make your code shorter and easier to read.
Later, when you work on line following, you will see code like this:
L, M, R = px.get_line_status() # L = left, M = middle, R = right
That line is using the same unpacking idea you just learned. The function returns three values, and Python puts them into the variables L, M, and R in order.
Why Lists Matter
Lists allow your program to:
- Store multiple sensor readings
- Compare different values
- Make decisions based on those values
Right now, you will focus on observing these values. Later, you will use them to control how your car moves.
What’s Next?
Soon, you will use these lists to help your car follow a line on the ground. You will learn how to access individual sensor values and use them to steer the car left or right.
For now, your goal is simple:
Understand what the numbers mean and how Python groups them together.