Looping Through Lists in Python: Iterating Over List Elements

thumb_up 1  ·  sell Python list iteration, Looping through elements in Python lists, Iterating over a Python list

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

It will produce the following 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

It will produce the following output −

lst[0]: 25
lst[1]: 12
lst[2]: 10
lst[3]: -21
lst[4]: 10
lst[5]: 100



The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back