How to iterate over list in python

From DevOps Notebook
Revision as of 23:06, 14 November 2019 by MilosZ (talk | contribs)

Iterate trough list with for loop in python

# example list
list = [1, 2, 22, 55, 29] 

# for loop 
for i in list: 
	print(i)


Using enumerate() in for loop with list in python

# example list
list = [1, 2, 22, 55, 29] 

# Using enumerate() with for loop 
for i, val in enumerate(list): 
    print (i, ",",val)


Using while loop to go over list in python

# example list
list = [1, 2, 22, 55, 29] 

# Getting length of list 
length = len(list) 
i = 0
   
# Iterating using while loop 
while i < length: 
    print(list[i]) 
    i += 1