Ad
  • Custom User Avatar
  • Default User Avatar

    Hi,

    I have one thing to add. If the list has already been created and used, then len() is an obvious option and it makes no sense so use sum(). Here this isn't the case.

    Compare the two:

    # normal case: len() is O(1) and sum() is O(n)
    
    >>> l = [x for x range(100000) if f(x)] # this is O(n)
    >>> len(l)  # this is fast as the list is built: O(1)
    ...
    >>> sum(l)  # this is slow(er) as it iterates the list: O(n)
    
    # this case: Using len() is O(n) and sum() is O(n) too
    
    len([x for x in range(100000) if f(x)])  # this is O(n) as it combines
    
    # [x for x in range(100000) if f(x)]     # which is O(n) <- this is the slow operation
    # len()                                  # which is O(1)
    
    sum(1 for x in range(100000) if f(x))    # this is O(n) too but
    #   \------------------------------/
    #          this is a generator           # doesn't create a list only iterates over ther range
                                             # therefore it's much more memory efficient(!)
    

    Here is an interesting article about time complexity in Python.

    Regards,

    suic

  • Default User Avatar

    Hi,

    I don't understand your comment. The point here isn't if len() is faster or slower than sum(). The point is that for len() you need to build the whole list which you immediately throw away. sum with a generator doesn't generate any list (=> memory efficient).

    Regards,

    suic