Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fibonacci function with list comprehension returns None

Returns [None,None,None,None,None]

def perimeter(num):
    lst = [1]
    return [lst.append(n+lst[lst.index(n)-1]) for n in lst if len(lst) <= num]

print(perimeter(5))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

lst.append(...) doesn’t return anything (more precisely, returns None).

Your function returns the list created with the list comprehenshion . This list contains 5 results of lst.append(...) – which are all None.


What you technically can do, is return lst from the function:

def perimeter(num):
    lst = [1]
    [lst.append(n+lst[lst.index(n)-1]) for n in lst if len(lst) <= num]
    return lst
print(perimeter(5))
[1, 2, 3, 5, 8, 13]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading