-1

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))
  • Here is an existing question about [creating the fibonacci series using a list comprehension](https://stackoverflow.com/q/42370456/14627505). – Vladimir Fokow Aug 18 '22 at 19:22
  • 2
    Does this answer your question? [Why does append() always return None in Python?](https://stackoverflow.com/questions/16641119/why-does-append-always-return-none-in-python) – mkrieger1 Aug 18 '22 at 19:33

1 Answers1

0

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]
Vladimir Fokow
  • 3,728
  • 2
  • 5
  • 27