How to Traverse a List in Python

List in Python are data structures used to store multiple data at the same time. There are multiple ways to iterate over a list in Python. Let’s see all the different ways to iterate over a list in Python.
In this article, we will learn to traverse the list in many ways.
1 . Using for loop
li = [1, 5, 3, 8, 4] for i in li: print(i)
Output:
1 5 3 8 4
2. Using For loop and range()
li = [1, 5, 3, 8, 4] for i in range(len(li)): print(li[i])
Output:
1 5 3 8 4
3. Using while loop
li = [1, 5, 3, 8, 4] i = 0 while i < len(li): print(li[i]) i += 1
Output:
1 5 3 8 4
4. Using list comprehension
li = [1, 5, 3, 8, 4] [print(i) for i in li]
Output:
1 5 3 8 4
Subscribe
Login
Please login to comment
0 Discussion