Course
Loop Lists
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
Loop Lists
You can traverse the items in a list with Python's for loop construct. The traversal can be done, using list as an iterator or with the help of index.
Syntax
Python list gives an iterator object. To iterate a list, use the for statement as follows
for obj in list: . . . . . .
Example 1
Take a look at the following example
lst = [25, 12, 10, -21, 10, 100]for num in lst: print (num, end = ' ')
Output
25 12 10 -21 10 100
Example 2
To iterate through the items in a list, obtain the range object of integers "0" to "len-1". See the following example −
lst = [25, 12, 10, -21, 10, 100]indices = range(len(lst))for i in indices: print ("lst[{}]: ".format(i), lst[i])
Output
lst[0]: 25lst[1]: 12lst[2]: 10lst[3]: -21lst[4]: 10lst[5]: 100
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.