There are several ways to traverse a list in Python:
##1. For loop traversallists = ["m1", 1900, "m2", 2000] for item in lists: print(item)
lists = ["m1", 1900, "m2", 2000] for item in lists: item = 0; print(lists)
['m1', 1900, 'm2', 2000]
lists = ["m1", 1900, "m2", 2000] count = 0 while count < len(lists): print(lists[count]) count = count + 1
for index in range(len(lists)): print(lists[index])
for val in iter(lists): print(val)
for i, val in enumerate(lists): print(i, val)
0 m1 1 1900 2 m2 3 2000
for i, el in enumerate(lists, 1): print(i, el)
1 m1 2 1900 3 m2 4 2000
Python Tutorial column to learn!
The above is the detailed content of What are the ways to traverse a list in Python?. For more information, please follow other related articles on the PHP Chinese website!