In this tutorial, learn how to loop over Python list variable. Loop through list variable in Python and print each element one by one. You have to use Python for loop and looping over a list variable and print it in the output.

The list variable is the variable whose values are comma separated. All the items are enclosed within the square brackets. To print all the variables, you have to pass the variable as an argument of the print statement. However, it prints all the variable in the comma separation and enclosed within the square bracket.

For Loop Over Python List Variable and Print All Elements

To get only the items and not the square brackets. You have to use the Python for loop. Inside the for loop, you have to print each item of a variable one by one in each line.

Output

Ram
Shyam
10
Bilal
13.2
Feroz

The above example prints each element of the Python list variable line by line. They have printed here without any square bracket or quotes. The print statement prints the single element in each line.

For Loop With Range of Length to Loop Through List in Python

You can add a range of length with for loop of Python. With range, you can print list elements between the given range. You can print the element in the range whatever you want.

Below are the different range of length to print the elements of the list in Python.

Print All Elements using For Loop Range
If you want to print all the elements of the list in Python. You have to use the below-given example using the range() function of Python.

Check the below Python coding to print your list element as per your requirement.

Output

Ram
Shyam
10
Bilal
13.2
Feroz

The above example print all the elements of the list element in the output. The above output contains all the element with a single element in each line.

Print All Element Except Last
To print all the elements of the list variable in Python. You have to use the below-given example which removes the last element.

Add -1 after the len() function of Python to remove the last element from the output. You will get all the elements of the list element except the last one.

Output

Ram
Shyam
10
Bilal
13.2

The above example prints all the elements except the last one in Python List variable.

Print All Element Except Last Two
To remove more elements from the output with List element of Python. You have to increase the value you have added after the len() function of Python.

You can increase this value to remove the number of elements from the output.

Output

Ram
Shyam
10
Bilal

The above example prints all the elements except the last two list elements.

While Loop Through Python List Variable to Print All Element

In addition to the above all, you can also use the while loop of Python to access and print each element. You have to use the below-given example to print all the items of the list element.

In while loop, you have to first initialize the variable to start the while loop. Inside the while loop, you also have to add the same variable with increment operator.

Output

Ram
Shyam
10
Bilal
13.2
Feroz

The above example using the while loop and prints all the elements in the output. Each item of the list element gets printed line by line.

For-in Loop to Looping Over Each Element of List in Python

For-in loop of Python is the same as the foreach loop of PHP. It prints all the elements of the list variable in the output. Use the below-given example to print each element using the for-in loop.

Output

Ram
Shyam
10
Bilal
13.2
Feroz

The above example contains the output which prints all the elements line by line.

You may also like to read.

Hope, you like this tutorial of how to loop through Python list variable. If you have any query regarding the tutorial, please comment below.

Also tell me, which method you are using to loop through each element of list variable in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *

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