# Map and Lambda Function in Python HackerRank Solution

#### ByBrokenprogrammers

Dec 10, 2022

Hello Programmers, In this post, you will know how to solve the Map and Lambda Function in Python HackerRank Solution. This problem is a part of the HackerRank Python Programming Series.

One more thing to add, donâ€™t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

## Map and Lambda Function in Python HackerRank Solution

### Problem

Let’s learn some new Python concepts! You have to generate a list of the first N fibonacci numbers, 0 being the first number. Then, apply the map function and a lambda expression to cube each fibonacci number and print the list.

ConceptThe map() function applies a function to every member of an iterable and returns the result. It takes two parameters: first, the function that is to be applied and secondly, the iterables.
Let’s say you are given a list of names, and you have to print a list that contains the length of each name.

>> print (list(map(len, ['Tina', 'Raj', 'Tom'])))
[4, 3, 3]

Lambda is a single expression anonymous function often used as an inline function. In simple words, it is a function that has only one line in its body. It proves very handy in functional and GUI programming.

>> sum = lambda a, b, c: a + b + c
>> sum(1, 2, 3)
6

Note :Lambda functions cannot use the return statement and can only have a single expression. Unlike def, which creates a function and assigns it a name, lambda creates a function and returns the function itself. Lambda can be used inside lists and dictionaries.

#### Input Format :

One line of input: an integer N.

• 0 <= N <= 15

#### Output Format :

A list on a single line containing the cubes of the first N fibonacci numbers.

5

[0, 1, 1, 8, 27]

#### Explanation :

The first 5 fibonacci numbers are [0, 1, 1, 2, 3] , and their cubes are [0, 1, 1, 8, 27].

### Map and Lambda Function in Python HackerRank Solutions

cube = lambda x: x ** 3
def fibonacci(n):
a, b, c = 0, 1, 1
for _ in range(n):
yield a
a, b = b, a + b
if __name__ == '__main__':
n = int(input())
print(list(map(cube, fibonacci(n))))

Disclaimer: The above Problem (Map and Lambda Function in Python) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.