Traversing & Accessing a List


☞ It can be done in two ways.

1. Using Index
2. Without Using Index

Using Index

Example 1 :
#Traversing a list using for loop
L = [10, 20, 30, 40]
for i in range(len(L)):
    print(L[i])
Output
 
10
20
30
40
Example 2 :
#Traversing a list using while loop
L = [10, 20, 30, 40]
i = 0
while i < len(L):
    print(L[i])
    i = i + 1
Output
 
10
20
30
40

Without Using Index

Example :
L = [10, 20, 30, 40]
for i in L:
    print(i)
Output
 
10
20
30
40