Friday, September 20, 2024

How you can Go A number of Arguments to the map() Operate in Python

Must read


Introduction

The purpose of Python, with its wealthy set of built-in features, is to permit builders to perform complicated duties with relative ease. One such highly effective, but usually ignored, perform is the map() perform. The map() perform will execute a given perform over a set of things, however how can we go further arguments to the supplied perform?

On this Byte, we’ll be exploring the map() perform and how one can successfully go a number of arguments to it.

The map() Operate in Python

The map() perform in Python is a built-in perform that applies a given perform to each merchandise of an iterable (like checklist, tuple and so on.) and returns a listing of the outcomes.

def sq.(quantity):
    return quantity ** 2

numbers = [1, 2, 3, 4, 5]
squared = map(sq., numbers)

print(checklist(squared))  # Output: [1, 4, 9, 16, 25]

On this snippet, we have outlined a perform sq.() that takes a quantity and returns its sq.. We then use the map() perform to use this sq.() perform to every merchandise within the numbers checklist.

Why Go A number of Arguments to map()?

You is likely to be questioning, “Why would I have to go a number of arguments to map()?” Nicely, there are situations the place you might need a perform that takes multiple argument, and also you wish to apply this perform to a number of units of knowledge concurrently.

Not each perform we offer to map() will take just one argument. What if, as a substitute of a squared perform, we’ve got a extra generic math.pow perform, and one of many arguments is what quantity to boost the merchandise to. How can we deal with a case like this?

Or perhaps you could have two lists of numbers, and also you wish to discover the product of corresponding numbers from these lists. That is one other case the place passing a number of arguments to map() can come be useful.

How you can Go A number of Arguments to map()

There are a couple of various kinds of instances through which you’d wish to go a number of arguments to map(), two of which we talked about above. We’ll stroll by each of these instances right here.

A number of Iterables

Passing a number of arguments to the map() perform is straightforward when you perceive how one can do it. You merely go further iterables after the perform argument, and map() will take gadgets from every iterable and go them as separate arguments to the perform.

Here is an instance:

def multiply(x, y):
    return x * y

numbers1 = [1, 2, 3, 4, 5]
numbers2 = [6, 7, 8, 9, 10]
end result = map(multiply, numbers1, numbers2)

print(checklist(end result))  # Output: [6, 14, 24, 36, 50]

Notice: Make it possible for the variety of arguments within the perform ought to match the variety of iterables handed to map()!

Within the instance above, we have outlined a perform multiply() that takes two arguments and returns their product. We then go this perform, together with two lists, to the map() perform. The map() perform applies multiply() to every pair of corresponding gadgets from the 2 lists, and returns a brand new checklist with the outcomes.

A number of Arguments, One Iterable

Persevering with with our math.pow instance, let’s have a look at how we will nonetheless use map() to run this perform on all gadgets of an array.

The primary, and doubtless easiest, means is to not use map() in any respect, however to make use of one thing like checklist comprehension as a substitute.

import math

numbers = [1, 2, 3, 4, 5]
res = [math.pow(n, 3) for n in numbers]

print(res) # Output: [1.0, 8.0, 27.0, 64.0, 125.0]

That is essentiall all map() actually is, however it’s not as compact and neat as utilizing a handy perform like map().

Now, let’s have a look at how we will really use map() with a perform that requires a number of arguments:

import math
import itertools

numbers = [1, 2, 3, 4, 5]
res = map(math.pow, numbers, itertools.repeat(3, len(numbers)))

print(checklist(res)) # Output: [1.0, 8.0, 27.0, 64.0, 125.0]

This may occasionally appear a bit extra sophisticated at first, however it’s really quite simple. We use a helper perform, itertools.repeat, to create a listing the identical size as numbers and with solely values of 3.

So the output of itertools.repeat(3, len(numbers)), when transformed to a listing, is simply [3, 3, 3, 3, 3]. This works as a result of we’re now passing two lists of the identical size to map(), which it fortunately accepts.

Conclusion

The map() perform is especially helpful when working with a number of iterables, as it might probably apply a perform to the weather of those iterables in pairs, triples, or extra. On this Byte, we have lined how one can go a number of arguments to the map() perform and how one can work with a number of iterables.



Supply hyperlink

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article