Skip to content

Iterate over list of strings Python | Example code

  • by

Using For loop, while loop or enumerate() function can easily Iterate over a list of strings in Python. You can use list comprehension to create a new list based on the original list of strings.

Example code Iterate over a list of strings in 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 or suggestions on this Python Iterate list topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading