In this article, we are going to see different methods for the string list iterations. The methods which we will discuss are given below:

  1. Using the for loop with the range function.
  2. Using the while loop.
  3. Using the comprehension method.
  4. Using the enumerate method.
  5. Using enumerate and format the output.

So, let’s discuss each method with their program.

Method 1: Using the for loop with range function

So, this method will iterate the string array using the for loop, which is very simple.

# python for_loop_with_range_function.py

str_list = [“New York”,“Los Angeles”,“Chicago”,“Houston”,“Phoenix”,


            “Philadelphia”]

for x in range(len(str_list)):


    print(str_list[x])

Output: python for_loop_with_range_function.py

New York


Los Angeles


Chicago


Houston


Phoenix


Philadelphia

Line 3: We created a list of string elements.

Line 6 to 7: We use the range function and the range value calculated from the len () method. Then we simply iterate each element from the list and print it on the screen shown above in the output.

Method 2: Using the while loop


Another way which we can use is to iterate the string list through the while loop. But this is not generally a programmer’s use because of the unnecessary use of the extra variables.

# python using_while_loop.py

”’


 Iterate the string list using the while loop


”’



i = 0


str_list = [“New York”,“Los Angeles”,“Chicago”,“Houston”,“Phoenix”,


           “Philadelphia”]


sizeofList = len(str_list)

while i < sizeofList:


    print(str_list[i])


    i = 1

Output: python using_while_loop.py

New York


Los Angeles


Chicago


Houston


Phoenix


Philadelphia

Line 6: We initialize a variable to compare in the while loop.

Line 7: We created a list of string elements.

Line 9: We find the length of the string list.

Line 10 to 12: We are checking if the initialization variable (i) is less than the length of the string list, then it will print the elements of the string list, else it will stop the loop.

Method 3: Using the comprehension method

The comprehension method is the most popular method, and most programmers would like to use it because it looks more pythonic and is very clean. The method to use the comprehension is given below.

# python list_comprehension.py

”’


 Iterate the string list using the list comprehension method


”’

str_list=[“New York”,“Los Angeles”,“Chicago”,“Houston”,“Phoenix”,


         “Philadelphia”]

[print(i) for i in str_list]

Output: python list_comprehensive.py

New York


Los Angeles


Chicago


Houston


Phoenix


Philadelphia

Line 7: We created a list of string elements.

Line 9: This line inside, we are running a for loop and along with the print statement in a single line. That’s why we called it a list comprehension method.

Method 4: Using the enumerate method

There is another method that we can use to print the elements along with their index value. In this method, we iterate the string list with another extra variable, IDX (we can keep the name of this variable anything). This variable will print the index value of that particular element. But caution, the first variable will print the index value of the element, and the second variable will print the element.

# python enumerate.py

”’


 Iterate the string list using enumerate


”’

str_list = [“New York”,“Los Angeles”,“Chicago”,“Houston”,“Phoenix”,


            “Philadelphia”]

# this will print elements along with their index value

for idx, word in enumerate(str_list):


    print(idx, word)

Output: python enumerate.py

0 New York

1 Los Angeles

2 Chicago

3 Houston

4 Phoenix

5 Philadelphia

Line 7: We created a list of string elements.

Line 11 to 12: We are using the enumerate () function, and in that function, we are passing the string list. Then we use two variables, idx, and word, which will print the index value and element, respectively. We can see that in the above output, there is some numeric value along with the element. This numeric value is the index position of the element in the list array. The above numeric output starts from 0 because the index value starts from 0 in the list.

Method 5: Using enumerate and format the output

We can also format the output while printing our element. So, in this below program, we are going to see how we can format the output.

# python format_string_list.py

”’


 Iterate the string list and format it


”’

str_list = [“New York”,“Los Angeles”,“Chicago”,“Houston”,“Phoenix”,


            “Philadelphia”]

for i, word in enumerate(str_list):


    print (“string[{}] = {}”.format(i, word))

Output: python format_string_list.py

string[0] = New York

string[1] = Los Angeles

string[2] = Chicago

string[3] = Houston

string[4] = Phoenix

string[5] = Philadelphia

Line 7: We created a list of string elements.

Line 11 to 12: In this line, we use the enumerate function that details already give in the above example. But here, we are also using the format function. The format function accepts variables and passes those variables in the same order to the curly braces {} as shows in the above program. The first variable is an index (i), and the second variable is an element value (word), so the first curly brace will get an index value, and the second curly brace will get an element value.

Conclusion:

In this article, we have seen all the different methods to iterate a string list. We have also seen some more advanced concepts, like the list comprehension method, which is most popular to use in a pythonic way. We have also seen some beginner-level iteration methods like while loop. The best way to iteration is list comprehension because we do not create too much code complexity with fewer variables. But sometimes, people get confused about the list comprehension method. So, it’s up to the user to use any method, and there are no restrictions. Only suggestions were given by the programmer that use fewer variables and iterations to make code fast.

The code for this article is available at the Github link:


https://github.com/shekharpandey89/string-list-iterations