Loop Lists

What You'll Learn:

In this tutorial, you'll discover various ways to loop through list items in Python. Looping allows you to process each item in a list, like counting or printing them one by one.

Loop Through a List

You can loop through the list items using a for loop. This method is straightforward and easy to understand.

Example Code:

python
thislist = ["apple", "banana", "cherry"]
for x in thislist:
    print(x)

Output:

apple
banana
cherry

What's Happening Here?

  • The for loop goes through each item in thislist and prints it.

Loop Through the Index Numbers

You can also loop through the list items by referring to their index number using the range() and len() functions.

Example Code:

python
thislist = ["apple", "banana", "cherry"]
for i in range(len(thislist)):
    print(thislist[i])

Output:

apple
banana
cherry

What's Happening Here?

  • The for loop uses range(len(thislist)) to create an iterable of index numbers and prints each item by its index.

Using a While Loop

You can loop through the list items using a while loop. This method is useful when you need more control over the loop process.

Example Code:

python
thislist = ["apple", "banana", "cherry"]
i = 0
while i < len(thislist):
    print(thislist[i])
    i += 1

Output:

apple
banana
cherry

What's Happening Here?

  • The while loop continues as long as the index i is less than the length of the list. It prints each item and increases i by 1 after each iteration.

Looping Using List Comprehension

List Comprehension offers a concise way to loop through lists. It's a shorthand method for writing for loops.

Example Code:

python
thislist = ["apple", "banana", "cherry"]
[print(x) for x in thislist]

Output:

apple
banana
cherry

What's Happening Here?

  • The list comprehension loops through thislist and prints each item.

Iterate Over Indices of a List Using range()

Introduction

In Python, you can use the range() function with a for loop to iterate over the indices of a list. This is a fundamental technique for accessing and manipulating elements of a list by their index.

Syntax: To iterate over the indices of a list my_list:

python
for i in range(len(my_list)):
    # your code

The len() function returns the number of elements in the list. When combined with range(), it creates a sequence of indices from 0 to len(my_list) - 1.

Examples

Iterate Over Indices of a List Using range(): This example demonstrates how to iterate over the indices of a list using a for loop and range().

Example Code:

python
my_list = ['apple', 'banana', 'cherry']

for i in range(len(my_list)):
    print(i)

Output:

0
1
2

Iterate Over Indices and Access List Elements: This example shows how to access the elements of the list using the indices in the for loop.

Example Code:

python
my_list = ['apple', 'banana', 'cherry']

for i in range(len(my_list)):
    print(my_list[i])

Output:

apple
banana
cherry

Try It Yourself: Fun Exercises

  1. List Your Favorite Fruits:
    • Create a list of your favorite fruits.
    • Loop through the list using a for loop and print each fruit.
  2. Index Your Favorite Movies:
    • Create a list of your favorite movies.
    • Use a while loop to print each movie by its index.

Exercises:

  1. Print Indices of a List: Write a program to print the indices of the list ['cat', 'dog', 'bird'] using range().

    python
    pets = ['cat', 'dog', 'bird']
    for i in range(len(pets)):
        print(i)
    
  2. Print Elements of a List Using Indices: Write a program to print the elements of the list ['red', 'green', 'blue'] using their indices.

    python
    colors = ['red', 'green', 'blue']
    for i in range(len(colors)):
        print(colors[i])
    
  3. Sum Elements of a List Using Indices: Write a program to sum the elements of the list [5, 10, 15, 20] using their indices.

    python
    numbers = [5, 10, 15, 20]
    total = 0
    for i in range(len(numbers)):
        total += numbers[i]
    print(total) 
    • Output: 50

Summary

In this Python tutorial, we learned various ways to loop through lists, including using a for loop, referring to index numbers, using a while loop, and utilizing list comprehension. Looping is an essential skill for processing and managing list items efficiently. Keep experimenting and have fun with lists in Python!. We learned how to iterate over the indices of a list using the range() function. This technique is useful for accessing and manipulating list elements by their index. Practice these examples to get comfortable with iterating over list indices in your Python programs!