When to use While, For, and Map for iterations in Python?

Python has a really sophisticated way of handling iterations. The only thing it does not have “GOTO Labels” which I think is good.

Let us compare the three common ways of iterations in Python: While, For and Map by the way of an example. Imagine that you have a list of numbers and you would like to find the square of each number.

nums = [1,2,3,5,10]
result = []
for num in nums:
    result.append(num*num)
print(result)

It would print [1, 4, 9, 25, 100]

Alternative form of iteration using for is

nums = [1,2,3,5,10]
result = [num*num for num in nums]
print(result)

Now, let us try to use while.

nums = [1,2,3,5,10]
result = []
i = 0
while i < len(nums):
    num = nums[i]
    result.append(num*num)
    i += 1

print(result)

Look at both pieces of code. The for loop iterates over a list and while loop runs as long as the condition is true. So, if you forgot to increase i, the condition will remain false forever and while the loop will not end ever. That’s why while is called an indefinite loop.

Not everything that can be achieved using while can be achieved using for. Everything that can be achieved using for can be achieved using while. while is way more powerful but with more power comes more risk.

So, if your task can be performed using for don’t use while.

Now, let’s take a look at map. The map is a very interesting way of running the independent operations on a list of things. I encountered the map paradigm around 18 years ago while doing grid computing using PERL programming language.

Let’s take a look at the code to solve the same problem as above – squaring each number in a list.

nums = [1,2,3,5,10]
def sq(x):
    return x*x

result = map(sq, nums)
print(result)

Here notice that to the function we are passing our function sq and our arraynums. The map function will run the sq function on each element of nums. Here it does not matter to us in what order will it run. Also, whether it uses different processors to run sq function on different parts of the list it would not impact our logic.

The map function is way better to use over for loop but it has constraint. If the next value depends upon the previous computation, we would not be able to use map. So, map can only solve some problems that for can solve. And for can solve some problems that while can.

Advantage of map is that your code will easily be parallelizable i.e. run on multiple computers or multiple processors if you are using map. The code with for loop and while loops we will have to rewrite the logic to make it parallelizable.

Let us take the case of the Fibonacci series. Can we achieve the following using map()? I don’t think so. If you can, please put it in the comments.

def fib(n):
    result = []
    first, second = 0, 1
    result.extend([first, second])
    for _ in range(n-1):
        newnum = first + second
        first, second = second, newnum
        result.append(newnum)
    print(result)
>>> fib(20)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]         

This prints first twenty numbers of Fibonacci series.

There are many examples which are very difficult to achieve using map.

As a conclusion, your order of preference of usage should be:

map > for > while

Prefer map over “for” loop. Prefer for over “while” loop.

I hope this discussion was useful to you. You can try it on CloudxLab right away.

Happy Learning!