Home > Article > Backend Development > How to loop through all elements of a list in python?
Method: First create a list ("stus = ['Monkey King','Zhu Bajie','Spider Spirit']"), and then traverse the list through a for loop ("for i in stus:print(i )").
Traverse the list: Output all elements
Traverse the list in sequence
Rendering:
Code:
# 创建列表 stus = ['孙悟空','猪八戒','沙和尚','唐僧','白骨精','蜘蛛精'] # 依次遍历列表 print(stus[0]) print(stus[1]) print(stus[2]) print(stus[3])
Traverse the list through a while loop
Rendering:
# 创建列表 stus = ['孙悟空','猪八戒','沙和尚','唐僧','白骨精','蜘蛛精'] # 通过while循环遍历列表 i = 0 while i < len(stus): print(stus[i]) i += 1
Traverse the list through a for loop The best traversal method
Rendering:
# 创建列表 stus = ['孙悟空','猪八戒','沙和尚','唐僧','白骨精','蜘蛛精'] # 通过for循环遍历列表 for i in stus: print(i)Recommended tutorial: "
python tutorial"
The above is the detailed content of How to loop through all elements of a list in python?. For more information, please follow other related articles on the PHP Chinese website!