Python Iterate List

Filed Under: Python

Python Lists basically serve the purpose of arrays and store data/elements in it.

Lists can be traversed through a number of ways.


Ways to iterate List in Python

  • By using for Loop
  • By using while Loop
  • By using List Comprehension
  • By using for loop and range() function
  • By using NumPy
  • By using enumerate() function

1. Iterating List using for Loop

Python for loop can be used to iterate through the List.

Example:

input_list = [10, "Safa", 15, "Aman", 1] 


for x in input_list: 
	print(x) 

Output:

10
Safa
15
Aman
1

2. Iteration of list through while Loop

Python while loop can be used to iterate through the list.

Example:

input_list = [10, "Safa", 15, "Aman", 1] 

length_list = len(input_list) 
x = 0
  
while x < length_list: 
    print(input_list[x]) 
    x += 1

Output:

10
Safa
15
Aman
1

3. List Comprehension to iterate Python List

Python List Comprehension can also be used to traverse through a list efficiently.

List Comprehension is an easier way to create and traverse a list.

Example:

input_list = [10, "Safa", 15, "Aman", 1] 
[print(x) for x in input_list] 

Output:

10
Safa
15
Aman
1

4. Python iterate List using for Loop and range() function

The range() method enables the user to create series of elements within a specified range.

Python for Loop along with range() function can be used to iterate through the list.

Example:

input_list = [10, "Safa", 15, "Aman", 1] 
length_list = len(input_list) 
   
for x in range(length_list): 
    print(input_list[x]) 

Output:

10
Safa
15
Aman
1

5. Python Iterate List using NumPy

Python NumPy is basically a library that can be used to perform manipulations and operations on huge amount of data, serving the functionality of arrays.

NumPy can be used to traverse list having huge amount of data.

Example:

import numpy as n

x = n.arange(12) 

 
x = x.reshape(3, 4) 


for i in n.nditer(x): 
	print(i) 

In the above example, numpy.arange(value) function helps return evenly spaced items in an array according to the argument value provided.

The reshape() function enables the user to provide a new shape to the existing array without changing the data inserted into it by providing the argument values to it.

The numpy.nditer is basically an iterator object that is used to traverse a list/array.

Output:

0
1
2
3
4
5
6
7
8
9
10
11

6. Python enumerate() function to iterate through a List

Python enumerate() function basically serves as an easy technique to traverse/iterate through a list.

Example:

input_list = [10, "Safa", 15, "Aman", 1] 
for x, result in enumerate(input_list): 
    print (x, ":",result) 

Output:

0 : 10
1 : Safa
2 : 15
3 : Aman
4 : 1

Iterating through multiple lists in a simultaneous manner

Python zip() function is used to traverse multiple lists simultaneously.

It basically takes the smaller of all the lists into consideration and gives the output accordingly.

The zip() function halts if anyone of the lists get exhausted or traversed.

Example:

import itertools  
  
age = [21, 28, 31] 
gender = ['Male', 'Female', 'Others'] 
city = ['Pune', 'Mumbai'] 
  

for (x, y, z) in zip(age, gender, city): 
    print (x, y, z) 
    

Output:

21 Male Pune
28 Female Mumbai

Conclusion

Thus, in this article, we have understood and implemented different ways to iterate through a Python List.


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages