How to Loop Over Python List Variable With Examples

In this tutorial, learn how to loop over Python list variable. Loop through the list variable in Python and print each element one by one. The short answer is to use the for loop to loop over a List variable and print it in the output.

You can also use the While loop of Python to traverse through all the elements of the List variable. The for loop is helpful to get the values within a certain specified range. The list variable is the variable whose values are comma-separated. All the items are enclosed within the square brackets ([]). Let’s find out with the examples given below.

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 are printed here without any square brackets or quotes. The print statement prints the single element in each line.

Bonus: download a Free Python cheat sheet that will show you 20+ most important examples to learn in Python.

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

You can add a range of lengths 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 ranges 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 prints all the elements of the list element in the output. The above output contains all the elements 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 the Python List variable.

Print All Element Except Last Two

To remove more elements from the output with the 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, 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 a 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 the 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 Through Each Element in Python

The 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.

DOWNLOAD Free PYTHON CHEAT SHEET

You May Also Like to Read

I hope you like this tutorial on how to loop through the Python list variable.

References