this post was submitted on 12 Apr 2025
192 points (90.7% liked)

Technology

69421 readers
2460 users here now

This is a most excellent place for technology news and articles.


Our Rules


  1. Follow the lemmy.world rules.
  2. Only tech related news or articles.
  3. Be excellent to each other!
  4. Mod approved content bots can post up to 10 articles per day.
  5. Threads asking for personal tech support may be deleted.
  6. Politics threads may be removed.
  7. No memes allowed as posts, OK to post as comments.
  8. Only approved bots from the list below, this includes using AI responses and summaries. To ask if your bot can be added please contact a mod.
  9. Check for duplicates before posting, duplicates may be removed
  10. Accounts 7 days and younger will have their posts automatically removed.

Approved Bots


founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] borokov@lemmy.world -5 points 2 weeks ago (3 children)

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.

[–] Kacarott@aussie.zone 2 points 2 weeks ago

No, len is a constant time operation, at least in most cases I believe.

[–] riodoro1@lemmy.world 1 points 2 weeks ago* (last edited 2 weeks ago) (2 children)

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 2 points 2 weeks ago (1 children)

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 2 points 2 weeks ago (1 children)

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

[–] ChaoticNeutralCzech@feddit.org 1 points 2 weeks ago

Yes. If it's empty. But in cases where you need to check, it might as well not be.

[–] ChaoticNeutralCzech@feddit.org 2 points 2 weeks ago

The list is not necessarily empty. If you were sure it was, why check?

Then please be less lazy next time.