Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Functional Python The Lambda Lambada Reduce

Ross Coe
Ross Coe
5,061 Points

Can you explain how an empty list can be passed in long_total and that's not seen as None?

these steps/checks in long_total are a little confusing...

if a is not None and books and books is not None and b is None:

  • you need the check 'and books' otherwise the long_total borks when b = books.pop(0) - IndexError: pop from empty list

and later if a is not None and b is None and not books or books is None:

  • surely if books is None - there are no books..?
def long_total(a=None, b=None, books=None):
    if a is None and b is None and books is None:
        return None
    if a is None and b is None and books is not None:
        a = books.pop(0) #car
        b = books.pop(0) #car(cdr)
        return long_total(a, b, books)
    if a is not None and books and books is not None and b is None: #need to check books otherwise error trying to pop books(0)
        b = books.pop(0) # is you've a returned value
        return long_total(a, b, books)
    if a is not None and b is not None and books is not None:
        return long_total(a+b, None, books) # if you have a product return without b to step above
    if a is not None and b is not None and not books:
        return long_total(a+b, None, None)
    if a is not None and b is None and not books or books is None:
        return a #ultimate answer/returned value

'''
nakalkucing
nakalkucing
12,964 Points

I was wondering why too. Would you mind answering Craig Dennis?

1 Answer

Roger Perelló
Roger Perelló
3,713 Points

While both are "falsy", an empty list is not equal to None. After all, and empty list is something.