Last Updated on July 30, 2021 by Roshan Parihar
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.
1 2 3 |
myList = ['Ram', 'Shyam', 10, 'Bilal', 13.2, 'Feroz']; for x in myList: print(x); |
Output
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.
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.
1 2 3 |
myList = ['Ram', 'Shyam', 10, 'Bilal', 13.2, 'Feroz']; for x in range(len(myList)): print(myList[x]) |
Output
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.
1 2 3 |
myList = ['Ram', 'Shyam', 10, 'Bilal', 13.2, 'Feroz']; for x in range(len(myList)-1): print(myList[x]) |
Output
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.
1 2 3 |
myList = ['Ram', 'Shyam', 10, 'Bilal', 13.2, 'Feroz']; for x in range(len(myList)-2): print(myList[x]) |
Output
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.
1 2 3 4 5 |
myList = ['Ram', 'Shyam', 10, 'Bilal', 13.2, 'Feroz']; x=0; while x < len(myList): print(myList[x]) x += 1 |
Output
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.
1 2 3 |
myList = ['Ram', 'Shyam', 10, 'Bilal', 13.2, 'Feroz']; for List in myList: print(List) |
Output
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
- How to concatenate two list variables in Python
- Check Data Type in Python With Easy Examples.
- How to resolve the error showing TypeError: ‘list’ object is not callable’ in Python
- How to loop through dictionary elements in Python
- Find the size of a list item using Python
I hope you like this tutorial on how to loop through the Python list variable.
References