• borokov@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    9 days ago

    Isn’t it because list is linked list, so to get the Len it has to iterate over the whole list whereas to get emptyness it just have to check if there is a 1st element ?

    I’ too lazy to read the article BTW.

    • riodoro1@lemmy.world
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      9 days ago

      So… it has to iterate over the whole empty list is what you’re saying? like once for every of the zero items in the list?

      • borokov@lemmy.world
        link
        fedilink
        English
        arrow-up
        2
        ·
        9 days ago

        Don’t know how list are implemented in Python. But in the dumb linked list implementation (like C++ std::list), each element has a “next” member that point the the next element. So, to have list length, you have to do (pseudo code, not actual python code):

        len = 0
        elt = list.fisrt
        while exist(elt):
            elt = elt.next
            len++
        return len
        

        Whereas to test if list is empty, you just have to:

        return exist(list.first)
        
        • riodoro1@lemmy.world
          link
          fedilink
          English
          arrow-up
          1
          ·
          9 days ago

          That’s exactly what I was getting at. Getting length of an empty list would not even enter the loop.