
- 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
Iterate over a list in Python
In this article, we will learn about iterating/traversing over a list in Python 3.x. Or earlier.
A list is an ordered sequence of elements. It is a non-scalar data structure and mutable in nature. A list can contain distinct data types in contrast to arrays storing elements belonging to the same data types.
Method 1 − Using iterable without index
Example
list_inp = ['t','u','t','o','r','i','a','l','s','p','o','i','n','t'] # Iterate over the list for value in list_inp: print(value, end='')
Method 2 − Using general way via index
Example
list_inp = ['t','u','t','o','r','i','a','l','s','p','o','i','n','t'] # Iterate over the list for value in range(0,len(list_inp)): print(list_inp[value], end='')
Method 3 − Using the enumerate type
Example
list_inp = ['t','u','t','o','r','i','a','l','s','p','o','i','n','t'] # Iterate over the list for value,char in enumerate(list_inp): print(char, end='')
Method 4 − Using negative indexes
Example
list_inp = ['t','u','t','o','r','i','a','l','s','p','o','i','n','t'] # Iterate over the list for value in range(-len(list_inp),0): print(list_inp[value], end='')
All the above four methods yeilds the output displayed below.
Output
tutorialspoint
Method 5 − Using sliced lists
Example
list_inp = ['t','u','t','o','r','i','a','l','s','p','o','i','n','t'] # Iterate over the list for value in range(1,len(list_inp)): print(list_inp[value-1:value], end='') print(list_inp[-1:])
Output
['t']['u']['t']['o']['r']['i']['a']['l']['s']['p']['o']['i']['n']['t']
Conclusion
In this article, we learnt about iteration/traversal over a list data type. Also, we learnt about various implementation techniques.
- Related Articles
- Iterate over a dictionary in Python
- Iterate over a set in Python
- How to iterate over a list in Java?
- How to iterate over a Java list?
- How to iterate over a C# list?
- Iterate over characters of a string in Python
- Program to iterate over a List using Java 8 Lambda
- How to iterate over a list of files with spaces in Linux?
- Python Program to Iterate Over an Array
- How to iterate through a list in Python?
- Iterate over lines from multiple input streams in Python
- How to Iterate over Tuples in Dictionary using Python
- How do I iterate over a sequence in reverse order in Python?
- How can I iterate over files in a given directory in Python?
- Python program to iterate over multiple lists simultaneously?

Advertisements