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)
Using while loop
list1 = ['Aa', 'Bb', 'Cc'] length = len(list1) i = 0 # Iterating using while loop while i < length: print(list1[i]) i += 1
Using list comprehension
This is a single-line solution.
list1 = ['Hello', 'Loop'] [print(i) for i in list1]
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)
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)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.