Friday, September 22, 2023
HomeProgrammingFind out how to Go A number of Arguments to the map()...

Find out how to Go A number of Arguments to the map() Perform in Python


Introduction

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

On this Byte, we’ll be exploring the map() operate and easy methods to successfully move a number of arguments to it.

The map() Perform in Python

The map() operate in Python is a built-in operate that applies a given operate to each merchandise of an iterable (like checklist, tuple and so forth.) 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 operate sq.() that takes a quantity and returns its sq.. We then use the map() operate to use this sq.() operate to every merchandise within the numbers checklist.

Why Go A number of Arguments to map()?

You is perhaps questioning, “Why would I must move a number of arguments to map()?” Properly, there are eventualities the place you may need a operate that takes multiple argument, and also you need to apply this operate to a number of units of information concurrently.

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

Or perhaps you could have two lists of numbers, and also you need 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.

Find out how to Go A number of Arguments to map()

There are a number of various kinds of circumstances during which you’d need to move a number of arguments to map(), two of which we talked about above. We’ll stroll by each of these circumstances right here.

A number of Iterables

Passing a number of arguments to the map() operate is easy when you perceive easy methods to do it. You merely move extra iterables after the operate argument, and map() will take gadgets from every iterable and move them as separate arguments to the operate.

This is an instance:

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

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

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

Word: Guarantee that the variety of arguments within the operate ought to match the variety of iterables handed to map()!

Within the instance above, we have outlined a operate multiply() that takes two arguments and returns their product. We then move this operate, together with two lists, to the map() operate. The map() operate 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 examine how we will nonetheless use map() to run this operate on all gadgets of an array.

The primary, and possibly easiest, manner 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, but it surely’s not as compact and neat as utilizing a handy operate like map().

Now, let’s examine how we will really use map() with a operate 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 will appear a bit extra sophisticated at first, but it surely’s really quite simple. We use a helper operate, 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() operate is especially helpful when working with a number of iterables, as it could actually apply a operate to the weather of those iterables in pairs, triples, or extra. On this Byte, we have lined easy methods to move a number of arguments to the map() operate and easy methods to work with a number of iterables.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments