Member-only story

Python Programming Language: How to Loop Through Lists

Jesse L
2 min readJun 28, 2022

--

Hi everyone, welcome back. In these examples, we will be going over how to loop through Lists in Python. Lists can be used to store a collection of data. A List in Python is mutable and can allow duplicate values. The elements within a List are indexed and ordered. With this introduction out of the way, let’s get into it.

Creating List

Let’s start by creating our List with populated values:

myList = [1, 2, 3]
print(myList)
Output:
[1, 2, 3]

So now, we have a populated List with a few elements. We can verify that it was successfully created and populated by printing the List and checking the output. Now, let’s see how we can loop through the List.

While Loop

Let’s try to use a while loop to iterate through the List and print each element individually. Let’s see an example:

myList = [1, 2, 3]

i = 0
while i < len(myList):
print(myList[i])
i = i + 1
Output:
1
2
3

As we can see in the example above, we used a while loop to iterate through the List. With each iteration, we printed the corresponding element to verify that we are successfully iterating through each element individually.

--

--

Jesse L
Jesse L

Written by Jesse L

Hi, I'm a passionate technology enthusiast and lifelong learner. Beyond my technical pursuits, I'm also passionate about sharing my enthusiasm with others.

No responses yet