Iterate over list of strings Python | Example code

Using For loop, while loop or enumerate() function can easily Iterate over list of strings in Python.

Example code Iterate over list of strings Python

Simple example codes multiple ways to iterate over a list in Python.

Using For loop

list = ['Aa', 'Bb', 'Cc']

# Using for loop
for i in list:
    print(i)

Output:

Iterate over list of strings Python

Using while loop

list1 = ['Aa', 'Bb', 'Cc']

length = len(list1)
i = 0

# Iterating using while loop
while i < length:
    print(list1[i])
    i += 1

Output:

Aa
Bb
Cc

Using list comprehension

This is a single-line solution.

list1 = ['Hello', 'Loop']

[print(i) for i in list1]

Output:

Hello
Loop

Using enumerate() function

With this method, you can get the index value on Iteration over the list of strings in Python.

list1 = ['Hello', 'Loop']

for i, val in enumerate(list1):
    print(i, ",", val)

Output:

0 , Hello
1 , Loop

Do comment if you have any doubts and suggestions on this Python Iterate list topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.