How to Iterate through a List without using the Increment Variable in Python


The iteration is a simple programming algorithm to take some steps to iterate all the elements present in a list. The main feature of iteration is to repeat the code of block multiple times. In Python, we have some built−in functions such as enumerate(), list(), map(), and, lambda will be used to iterate through a list without using the increment variable.

Syntax

The following syntax is used in the examples

enumerate()

The enumerate() is an in−built function in Python that can be used to track the specific iteration.

list()

The list() is a built−in method in Python that creates the list object.

map()

The built−in method map() generates the process for a specific function where each element is in an iterable state.

lambda

The function lambda offers a shortcut for declaring brief anonymous functions using the lambda keyword. The lambda has only one expression argument.

len()

The built−in method len() can be used to return the length of the object.

Using enumerate() function

In the following example, we will start the program by creating list in the variable list_1. Then use the for loop, where variable i iterate through the input list by using built−in function enumerate() and store the iteration result in variable k.

Example

list_1 = ["A", 10, "B", 20, "C", 30, "D", 40]
for i, k in enumerate(list_1): 
    print (i,":", k)

Output

0 : A
1 : 10
2 : B
3 : 20
4 : C
5 : 30
6 : D
7 : 40

Using map() and lambda() function

In the following example, the program start by storing the input list in the variable inp_list. Next, it will use the built−in function lamda that calculates the iteration without any incrementing operator. It then set with built−in method map() and list() to extract the result in the form of a list.

Example

inp_list = [1, 2, 3, 4, 5] 
rlt = list(map(lambda i:i, inp_list))
print(rlt) 

Output

[1, 2, 3, 4, 5]

Using List Comprehension

In the following example, we will start the program by storing the string list in the variable list_1. It then directly uses the list comprehension where variable i iterate through the input list and display the result using the same variable i.

Example

list_1 = ["Welcome", "To", "Tutorialspoint"] 
[print(i) for i in list_1] 

Output

Welcome
To
Tutorialspoint

Using range() function

In the following example, the program uses for loop where built−in functions len() and range() will help to iterate a list without using any incremental variable and generate the output.

Example

inp_list = ['A', 'B', 'C', 'D', 'E']
for i in range(len(inp_list)): 
    print(inp_list[i]) 

Output

A
B
C
D
E

Using numpy library

In the following example, we will first import the module named numpy and take the object reference as np. Then use the built−in method array with np that creates the array and store it in the variable inp_list. Next, using for loop, variable i iterate through the input list and display the result with the help of variable i.

Example

import numpy as np
inp_list = np.array([1, 2, 3, 4, 5])
for i in inp_list:
  print(i)

Output

1
2
3
4
5

Conclusion

We discussed the various example to explore the different methods to solve this problem statement. As we know, iteration plays a crucial role in every aspect of programming language but when it comes to Python programming it always takes the chance to use the built−in function and make the code shorter.

Updated on: 14-Aug-2023

103 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements