![]() ![]() So far we've passed into map() functions that take only one argument (recall the cube(num)). ![]() Print(fin_list) # How to Use Functions with Multiple Iterables in Python For example if you had a list of strings, you can easily create a new list with the length of each string in the list. You can also pass in built-in Python functions. Much cleaner, wouldn't you agree? How to Use Built-in Functions in Python Here's how you'd do that: fin_list = list(map(lambda x:x**3, org_list)) Instead of writing a separate function to calculate the cube of a number, we can use a lambda expression in its place. It finally returned a new iterable ( fin_list ) with the result. In case you're wondering what went on behind the scenes, the map() function essentially iterated through each element of the iterable (in our case, org_list) and applied the cube function on it. # define a function that returns the cube of `num`ĭon't know about you, but I find this to be much cleaner logic. Which is perfectly valid, but let's see how using the map() function simplifies your code: org_list = A traditional approach would involve using the for loop: org_list = Let's see an example: imagine you have a list of numbers, and you want to create a new list with the cubes of the numbers in the first list. The general syntax for this is: map(function, iterable, ) It returns a new iterable (a map object) that you can use in other parts of your code. The map() function (which is a built-in function in Python) is used to apply a function to each item in an iterable (like a Python list or dictionary). In this article, we'll explore what the map() function is and how to use it in your code. And the most notable one is the map() function. Python offers a number of functional programming utilities even though it's primarily an object-oriented programming language. ![]()
0 Comments
Leave a Reply. |