
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to iterate through a list in Python?
There are different ways to iterate through a list object. The for statement in Python has a variant which traverses a list till it is exhausted. It is equivalent to foreach statement in Java. Its syntax is −
for var in list: stmt1 stmt2
Example
Following script will print all items in the list
L=[10,20,30,40,50] for var in L: print (L.index(var),var)
Output
The output generated is −
0 10 1 20 2 30 3 40 4 50
Example
Another approach is to iterate over range upto length of list, and use it as index of item in list
for var in range(len(L)): print (var,L[var])
Output
You can also obtain enumerate object from the list and iterate through it. Following code too gives same output.
for var in enumerate(L): print (var)
- Related Articles
- How to iterate through Java List?
- How we can iterate through a Python list of tuples?
- How to iterate through a dictionary in Python?
- How to iterate through a tuple in Python?
- Iterate over a list in Python
- How to iterate a list in Java?
- How to iterate a Java List?
- How to correctly iterate through getElementsByClassName() in JavaScript?
- How to iterate over a list in Java?
- How can I iterate through two lists in parallel in Python?
- How to iterate over a Java list?
- How to iterate over a C# list?
- How to iterate a Java List using Iterator?
- Python - Ways to iterate tuple list of lists
- Iterate through ArrayList in Java

Advertisements