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:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
Output:
apple
banana
cherryWhat's Happening Here?
- The for loop goes through each item in
thislistand 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:
thislist = ["apple", "banana", "cherry"]
for i in range(len(thislist)):
print(thislist[i])
Output:
apple
banana
cherryWhat'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:
thislist = ["apple", "banana", "cherry"]
i = 0
while i < len(thislist):
print(thislist[i])
i += 1
Output:
apple
banana
cherryWhat's Happening Here?
- The while loop continues as long as the index
iis less than the length of the list. It prints each item and increasesiby 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:
thislist = ["apple", "banana", "cherry"]
[print(x) for x in thislist]
Output:
apple
banana
cherryWhat's Happening Here?
- The list comprehension loops through
thislistand 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:
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:
my_list = ['apple', 'banana', 'cherry']
for i in range(len(my_list)):
print(i)
Output:
0
1
2Iterate 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:
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
- List Your Favorite Fruits:
- Create a list of your favorite fruits.
- Loop through the list using a for loop and print each fruit.
- Index Your Favorite Movies:
- Create a list of your favorite movies.
- Use a while loop to print each movie by its index.
Exercises:
Print Indices of a List: Write a program to print the indices of the list
['cat', 'dog', 'bird']usingrange().pythonpets = ['cat', 'dog', 'bird'] for i in range(len(pets)): print(i)Print Elements of a List Using Indices: Write a program to print the elements of the list
['red', 'green', 'blue']using their indices.pythoncolors = ['red', 'green', 'blue'] for i in range(len(colors)): print(colors[i])Sum Elements of a List Using Indices: Write a program to sum the elements of the list
[5, 10, 15, 20]using their indices.pythonnumbers = [5, 10, 15, 20] total = 0 for i in range(len(numbers)): total += numbers[i] print(total)- Output:
50
- Output:
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!