Home > front end >  Python program to print fibonacci series using lambda function in Python
Python program to print fibonacci series using lambda function in Python

Time:10-19

I have found this code online but not sure what does "any" do there!?

def fibonacci(count):
    fib_list = [0, 1]
    any(map(lambda _: fib_list.append(sum(fib_list[-2:])),
                                     range(2, count)))
    return fib_list[:count]
print(fibonacci(20))

CodePudding user response:

In essence, the any function will keep looping through the iterable input until a value is true. The input is the result of map which won't ever be True since fib_list.append is always None.

CodePudding user response:

If you want to see the code you can just tuple or list and print the map values:

    def fibonacci(count):
    fib_list = [0, 1]
    print(tuple(map(lambda _: fib_list.append(sum(fib_list[-2:])),range(2, count))))
    return fib_list[:count]
    print(fibonacci(20))

    #output
    (None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None)
    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]

You can skip any if you want and replace it with a tuple or list

Link to any documentation https://docs.python.org/3/library/functions.html?highlight=any#any

  • Related