Lemmy.ca
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
abhi9u@lemmy.world to Technology@lemmy.worldEnglish · 26 days ago

Python Performance: Why 'if not list' is 2x Faster Than Using len()

blog.codingconfessions.com

external-link
message-square
150
link
fedilink
  • cross-posted to:
  • [email protected]
196
external-link

Python Performance: Why 'if not list' is 2x Faster Than Using len()

blog.codingconfessions.com

abhi9u@lemmy.world to Technology@lemmy.worldEnglish · 26 days ago
message-square
150
link
fedilink
  • cross-posted to:
  • [email protected]
Discover why 'if not mylist' is twice as fast as 'len(mylist) == 0' by examining CPython's VM instructions and object memory access patterns.
  • Sirber
    link
    fedilink
    English
    arrow-up
    43
    arrow-down
    1
    ·
    edit-2
    26 days ago

    How does Python know if it’s my list or not?

    • dblsaiko@discuss.tchncs.de
      link
      fedilink
      English
      arrow-up
      28
      ·
      26 days ago

      Telemetry

    • JasonDJ@lemmy.zip
      link
      fedilink
      English
      arrow-up
      8
      arrow-down
      1
      ·
      26 days ago

      if isinstance(mylist, list) and not mylist

      Problem solved.

      Or if not mylist # check if list is empty

      • Sirber
        link
        fedilink
        English
        arrow-up
        15
        ·
        26 days ago

        I think you missed the joke 😅

        • PattyMcB@lemmy.world
          link
          fedilink
          English
          arrow-up
          3
          ·
          26 days ago

          I thought it was funny!

      • gravitas_deficiency@sh.itjust.works
        link
        fedilink
        English
        arrow-up
        4
        ·
        26 days ago

        You’re checking if mylist is falsey. Sometimes that’s the same as checking if it’s empty, if it’s actually a list, but that’s not guaranteed.

        • JasonDJ@lemmy.zip
          link
          fedilink
          English
          arrow-up
          1
          ·
          26 days ago

          Doesn’t Python treat all empty iterables as false tho? This isn’t unique to python, is it? (though I’m not a programmer…just a dude who writes scripts every now and then)

          • gravitas_deficiency@sh.itjust.works
            link
            fedilink
            English
            arrow-up
            3
            ·
            26 days ago

            My point is that the second statement you presented can have the effect of evaluating emptiness of a Sequence (note: distinct from an Iterable), but that only holds true if the target of the conditional IS a sequence. I’m underlining the semantic difference that was elided as a result of falsey evaluation.

            • JasonDJ@lemmy.zip
              link
              fedilink
              English
              arrow-up
              1
              ·
              26 days ago

              Ok, help a noob out. What is the difference between a sequence and an iterable? Is a sequence immutable, like a tuple?

              • gravitas_deficiency@sh.itjust.works
                link
                fedilink
                English
                arrow-up
                1
                ·
                edit-2
                26 days ago

                thing: Sequence[Any] iirc is iterable, indexable, and reversible.

                thing: Iterable[Any] only guarantees that its iterable - and note that iterating can sometimes have the effect of consuming the iterable (e.g. when working with streaming interfaces)

              • 48954246@lemmy.world
                link
                fedilink
                English
                arrow-up
                1
                ·
                26 days ago

                An iterable is just something that can be iterated over, like range(10), or [1, 2, 3].

                A sequence on the other hand is a Collection that is reversible.

                https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes

                • gravitas_deficiency@sh.itjust.works
                  link
                  fedilink
                  English
                  arrow-up
                  1
                  ·
                  26 days ago

                  I know what an iterable is. But I am talking about Type[Iterable], which iirc does not obey falsey eval when empty.

          • BlackRoseAmongThorns@slrpnk.net
            link
            fedilink
            English
            arrow-up
            2
            ·
            26 days ago

            Not really, generators have weird truthiness, i don’t remember if they evaluate to true or false, but they cannot be checked for emptiness so they default to either always true or always false.

    • jj4211@lemmy.world
      link
      fedilink
      English
      arrow-up
      1
      ·
      25 days ago

      else: # not my list, it is ourlist

    • gargolito@lemm.ee
      link
      fedilink
      English
      arrow-up
      1
      ·
      26 days ago

      Python likes giving lists.

Technology@lemmy.world

technology@lemmy.world

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: [email protected]

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


  • @[email protected]
  • @[email protected]
  • @[email protected]
  • @[email protected]
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 3.5K users / day
  • 9.64K users / week
  • 18K users / month
  • 37.5K users / 6 months
  • 1.41K local subscribers
  • 69.8K subscribers
  • 14.9K Posts
  • 646K Comments
  • Modlog
  • mods:
  • L3s@lemmy.world
  • enu@lemmy.world
  • Technopagan@lemmy.world
  • L4sBot@lemmy.world
  • L3s@hackingne.ws
  • L4s@hackingne.ws
  • BE: 0.19.11
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org