
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Nested For Loop to Map Equivalent in Python
In general, for loop is used to execute/iterate a block of code a fixed number of times. And nested for loop is nothing but, iterating a block of code for x number of times, then we need to run another block of code within that code for y number of times.
Nested for loop syntax
for x in sequence: for y in sequence: inner loop outer loop
The map is a built-in function in python that is used to iterate the items of a sequence and generates results after applying a function on those items.
Syntax
map(function, iterable)
Where,
function: this function will be applied to the items of the iterable.
iterable: a sequence object like list, set, tuple, etc.,
In this article, we will see python programs to convert a nested for loop to a map equivalent.
Example
Let us take a nested for-loop example to change the data type of a two-dimensional array elements. then, convert a map equivalent to the for-loop.
a = [['2.3','.2'],['-6.3','0.9']] print("Input:", a) for j in range(2): for i in range(2): a[i][j] = float(a[i][j]) print("Nested for-loop output",a) # Create a map equivalent to the above code a = [['2.3','.2'],['-6.3','0.9']] print("Input:", a) result = [list(map(float, subarr)) for subarr in a] print("Converted Map equivalent output:",result)
Output
Input: [['2.3', '.2'], ['-6.3', '0.9']] Nested for-loop output [[2.3, 0.2], [-6.3, 0.9]] Input: [['2.3', '.2'], ['-6.3', '0.9']] Converted Map equivalent output: [[2.3, 0.2], [-6.3, 0.9]]
We can see both the nested for-loop and map equivalent codes with their results in the above blocks. Here we have changed the datatype of the two-dimensional array elements from string to floating point numbers.
Using list comprehension and map() function we have converted the nested for-loop to the map equivalent.
Example
Another approach to the above problem here will utilize the lambda operator to map float to the sub-lists. And replaced the list comprehension by using the map() function twice for iterating the list and sub-list elements.
a = [['2.3','.2'],['-6.3','0.9']] print("Input:", a) result = list(map(lambda b : list(map(float, b)), a)) print("Converted Map equivalent output:",result)
Output
Input: [['2.3', '.2'], ['-6.3', '0.9']] Converted Map equivalent output: [[2.3, 0.2], [-6.3, 0.9]]
Example
Let us take an example to find the list of all the prime numbers within a range using nested for loop and map() function.
# Python program to find list of all the prime numbers within a range lower = 2 upper = 30 prime_numbers = [] for num in range(lower, upper + 1): # all prime numbers are greater than 1 if num > 1: for i in range(2, num): if (num % i) == 0: break else: prime_numbers.append(num) print("List of prime numbers using Nested for-loop:") print(prime_numbers) # find list of prime numbers using map map_resul = list(filter(None, list(map(lambda i: i if all(i%j != 0 for j in range(2, int(i ** 0.5) + 1)) else None , range(lower, upper + 1))))) print("List of prime numbers using Map equivalent to the nestesd for-loop:") print(map_resul)
Output
List of prime numbers using Nested for-loop: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] List of prime numbers using Map equivalent to the nestesd for-loop: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
We can see both the nested for-loop and map equivalent codes to find the list of all prime numbers between the given range.
The map equivalent code is implemented by using the lambda with if else blocks and a filter functions.
Example
In this example, we will write the nested for-loop and map equivalent codes to add the 2 integers of the loop elements.
forloop_result = [] for a in range(1,5): for b in range(1,6): forloop_result.append(a+b) print("Nested for-loop output", forloop_result) from itertools import product result = list(map(lambda x: sum(x) , product(range(1,5), range(1,6)))) print("Converted Map equivalent output:",result)
Output
Nested for-loop output [2, 3, 4, 5, 6, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8, 5, 6, 7, 8, 9] Converted Map equivalent output: [2, 3, 4, 5, 6, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8, 5, 6, 7, 8, 9]
Here, executed the product() method from the itertools module is used to get the Cartesian product of input iterables. like below,
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5)]