Count, Collect, and Organize
Explore how to create and manage lists in Python to store multiple values together. Learn to access items using zero-based indexing, modify lists with methods like append and remove, and iterate through list elements with for loops. This lesson helps you organize data effectively and automate repetitive tasks in your code.
We'll cover the following...
Sometimes, we need to keep track of many values, like scores, names, or steps. Instead of using a new variable for each one, we can use a list to store them all together in a neat, single container.
They let us store and organize many values in one place, so our code can handle more with less effort.
Creating a list of values
A list is like a container containing multiple items—such as numbers, words, or other lists. We can create a list like this:
We just created our first list. A list holds a group of values in one variable.
Accessing specific items in a list
We can access items using their position (called an index). An index is the position number of an item in a list— it tells Python where to look.
In Python, indexing starts at 0, so the first item is at index 0, the second at 1, and so on.
Python uses zero-based indexing, so the first item is at position 0. We just learned how to access a specific list item!
Modifying a list
We can also modify items. We may need to change the data in a list sometimes.
For example:
We use
.append()to add new items as they come in (like new scores or messages).We use
.remove()to take out items that are no longer needed.
This keeps our list updated and our program flexible.
Lists can grow or shrink by using methods like append() and remove(). We now know how to add or remove a specific item from the list!
Looping through a list
We can also loop through all items. To understand how this code works step by step, let's look at the following flowchart.
This will help you visualize the flow of control in your code—not just what runs but also in what order and how data moves.
The list
fruitsholds three items:"apple","banana", and"cherry".The
forloop goes through each item one by one—this is called iteration.At each step, the loop picks the next fruit and runs the
print()line.
The flowchart is meant to help you see the order of steps Python follows, including looping back until all items are processed. Look closely at how Python checks if it has reached the end of the list before stopping.
Earlier, we used a for loop with range() to repeat something a certain number of times:
for i in range(5):print("Hello!")
Here, Python counts from 0 to 4 and runs the loop 5 times.
But now, we’re using a different kind of for loop that goes through each item in a list:
for fruit in fruits:print("I love", fruit)
Instead of counting, this loop picks one item at a time from the list fruits and runs the loop once for each item. It’s a simpler and more natural way to work with lists.
First iteration:
fruit = "apple"Second iteration:
fruit = "banana"Third iteration:
fruit = "cherry"
In a for loop, Python automatically moves through each item in the list, starting from the first one, and stops when it reaches the end. After the third iteration, no more items are in the list, so the loop ends.
We used a for loop to go through each item in the list.