

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 correctly iterate through getElementsByClassName() in JavaScript?
- How to iterate a Java List?
- Iterate through ArrayList in Java
- How to iterate 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?
- Iterate through Java Unit Tuple
- Iterate through Java Pair Tuple
- How to iterate over a list in Java?
Advertisements