Languages
[Edit]
EN

Python - literate through list

0 points
Created by:
Palus-Bear
1016

In this article, we would like to show you how to iterate through the list in Python.

1. Using for loop

In this example, we use simple for loop to iterate through the my_list and print its items.

my_list = ['A', 'B', 'C']

for x in my_list:
    print(x)

Output:

A
B
C

2. Using while loop

To iterate through the list using while loop, we need to:

  • specify the iterator - in our case we set i = 0,
  • use len() function to specify the length of the list.
  • increment the iterator (i) after each iteration.

Practical example:

my_list = ['A', 'B', 'C']

i = 0
while i < len(my_list):
    print(my_list[i])
    i = i + 1

Output:

A
B
C

Alternative titles

  1. Python - loop through the list
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Python - list (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join