How to iterate over list in python

From DevOps Notebook
Revision as of 23:10, 14 November 2019 by MilosZ (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

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


Using while loop to go over list in python

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

Loop over multiple lists at the same time with zip

for i, i2 in zip(list, list2):
    print("{}: {}".format(i, ", ".join(i2)))