▲ 197 ▼ Python Performance: Why 'if not list' is 2x Faster Than Using len() (blog.codingconfessions.com) submitted 1 year ago by abhi9u@lemmy.world to c/technology@lemmy.world 145 comments fedilink hide all child comments
[–] sirber@lemmy.ca 42 points 1 year ago* (last edited 1 year ago) (4 children) How does Python know if it's my list or not? permalink fedilink source hideshow 8 child comments replies: [–] 2xsaiko@discuss.tchncs.de 28 points 1 year ago Telemetry permalink fedilink source parent [–] JasonDJ@lemmy.zip 7 points 1 year ago (2 children) if isinstance(mylist, list) and not mylist Problem solved. Or if not mylist # check if list is empty permalink fedilink source parent hideshow 4 child comments replies: [–] sirber@lemmy.ca 15 points 1 year ago (1 child) I think you missed the joke 😅 permalink fedilink source parent hideshow 2 child comments replies: [–] PattyMcB@lemmy.world 3 points 1 year ago I thought it was funny! permalink fedilink source parent [–] gravitas_deficiency@sh.itjust.works 4 points 1 year ago (1 child) 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. permalink fedilink source parent hideshow 2 child comments replies: [–] JasonDJ@lemmy.zip 1 point 1 year ago (2 children) 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) permalink fedilink source parent hideshow 4 child comments replies: [–] gravitas_deficiency@sh.itjust.works 3 points 1 year ago (1 child) 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. permalink fedilink source parent hideshow 2 child comments replies: [–] JasonDJ@lemmy.zip 1 point 1 year ago (2 children) Ok, help a noob out. What is the difference between a sequence and an iterable? Is a sequence immutable, like a tuple? permalink fedilink source parent hideshow 4 child comments replies: [–] 48954246@lemmy.world 1 point 1 year ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] gravitas_deficiency@sh.itjust.works 1 point 1 year ago I know what an iterable is. But I am talking about Type[Iterable], which iirc does not obey falsey eval when empty. permalink fedilink source parent [–] gravitas_deficiency@sh.itjust.works 1 point 1 year 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) permalink fedilink source parent [–] BlackRoseAmongThorns@slrpnk.net 2 points 1 year 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. permalink fedilink source parent [–] gargolito@lemm.ee 1 point 1 year ago Python likes giving lists. permalink fedilink source parent [–] jj4211@lemmy.world 1 point 1 year ago else: # not my list, it is ourlist permalink fedilink source parent
[–] JasonDJ@lemmy.zip 7 points 1 year ago (2 children) if isinstance(mylist, list) and not mylist Problem solved. Or if not mylist # check if list is empty permalink fedilink source parent hideshow 4 child comments replies: [–] sirber@lemmy.ca 15 points 1 year ago (1 child) I think you missed the joke 😅 permalink fedilink source parent hideshow 2 child comments replies: [–] PattyMcB@lemmy.world 3 points 1 year ago I thought it was funny! permalink fedilink source parent [–] gravitas_deficiency@sh.itjust.works 4 points 1 year ago (1 child) 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. permalink fedilink source parent hideshow 2 child comments replies: [–] JasonDJ@lemmy.zip 1 point 1 year ago (2 children) 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) permalink fedilink source parent hideshow 4 child comments replies: [–] gravitas_deficiency@sh.itjust.works 3 points 1 year ago (1 child) 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. permalink fedilink source parent hideshow 2 child comments replies: [–] JasonDJ@lemmy.zip 1 point 1 year ago (2 children) Ok, help a noob out. What is the difference between a sequence and an iterable? Is a sequence immutable, like a tuple? permalink fedilink source parent hideshow 4 child comments replies: [–] 48954246@lemmy.world 1 point 1 year ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] gravitas_deficiency@sh.itjust.works 1 point 1 year ago I know what an iterable is. But I am talking about Type[Iterable], which iirc does not obey falsey eval when empty. permalink fedilink source parent [–] gravitas_deficiency@sh.itjust.works 1 point 1 year 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) permalink fedilink source parent [–] BlackRoseAmongThorns@slrpnk.net 2 points 1 year 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. permalink fedilink source parent
[–] sirber@lemmy.ca 15 points 1 year ago (1 child) I think you missed the joke 😅 permalink fedilink source parent hideshow 2 child comments replies: [–] PattyMcB@lemmy.world 3 points 1 year ago I thought it was funny! permalink fedilink source parent
[–] PattyMcB@lemmy.world 3 points 1 year ago I thought it was funny! permalink fedilink source parent
[–] gravitas_deficiency@sh.itjust.works 4 points 1 year ago (1 child) 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. permalink fedilink source parent hideshow 2 child comments replies: [–] JasonDJ@lemmy.zip 1 point 1 year ago (2 children) 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) permalink fedilink source parent hideshow 4 child comments replies: [–] gravitas_deficiency@sh.itjust.works 3 points 1 year ago (1 child) 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. permalink fedilink source parent hideshow 2 child comments replies: [–] JasonDJ@lemmy.zip 1 point 1 year ago (2 children) Ok, help a noob out. What is the difference between a sequence and an iterable? Is a sequence immutable, like a tuple? permalink fedilink source parent hideshow 4 child comments replies: [–] 48954246@lemmy.world 1 point 1 year ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] gravitas_deficiency@sh.itjust.works 1 point 1 year ago I know what an iterable is. But I am talking about Type[Iterable], which iirc does not obey falsey eval when empty. permalink fedilink source parent [–] gravitas_deficiency@sh.itjust.works 1 point 1 year 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) permalink fedilink source parent [–] BlackRoseAmongThorns@slrpnk.net 2 points 1 year 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. permalink fedilink source parent
[–] JasonDJ@lemmy.zip 1 point 1 year ago (2 children) 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) permalink fedilink source parent hideshow 4 child comments replies: [–] gravitas_deficiency@sh.itjust.works 3 points 1 year ago (1 child) 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. permalink fedilink source parent hideshow 2 child comments replies: [–] JasonDJ@lemmy.zip 1 point 1 year ago (2 children) Ok, help a noob out. What is the difference between a sequence and an iterable? Is a sequence immutable, like a tuple? permalink fedilink source parent hideshow 4 child comments replies: [–] 48954246@lemmy.world 1 point 1 year ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] gravitas_deficiency@sh.itjust.works 1 point 1 year ago I know what an iterable is. But I am talking about Type[Iterable], which iirc does not obey falsey eval when empty. permalink fedilink source parent [–] gravitas_deficiency@sh.itjust.works 1 point 1 year 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) permalink fedilink source parent [–] BlackRoseAmongThorns@slrpnk.net 2 points 1 year 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. permalink fedilink source parent
[–] gravitas_deficiency@sh.itjust.works 3 points 1 year ago (1 child) 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. permalink fedilink source parent hideshow 2 child comments replies: [–] JasonDJ@lemmy.zip 1 point 1 year ago (2 children) Ok, help a noob out. What is the difference between a sequence and an iterable? Is a sequence immutable, like a tuple? permalink fedilink source parent hideshow 4 child comments replies: [–] 48954246@lemmy.world 1 point 1 year ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] gravitas_deficiency@sh.itjust.works 1 point 1 year ago I know what an iterable is. But I am talking about Type[Iterable], which iirc does not obey falsey eval when empty. permalink fedilink source parent [–] gravitas_deficiency@sh.itjust.works 1 point 1 year 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) permalink fedilink source parent
[–] JasonDJ@lemmy.zip 1 point 1 year ago (2 children) Ok, help a noob out. What is the difference between a sequence and an iterable? Is a sequence immutable, like a tuple? permalink fedilink source parent hideshow 4 child comments replies: [–] 48954246@lemmy.world 1 point 1 year ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] gravitas_deficiency@sh.itjust.works 1 point 1 year ago I know what an iterable is. But I am talking about Type[Iterable], which iirc does not obey falsey eval when empty. permalink fedilink source parent [–] gravitas_deficiency@sh.itjust.works 1 point 1 year 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) permalink fedilink source parent
[–] 48954246@lemmy.world 1 point 1 year ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] gravitas_deficiency@sh.itjust.works 1 point 1 year ago I know what an iterable is. But I am talking about Type[Iterable], which iirc does not obey falsey eval when empty. permalink fedilink source parent
[–] gravitas_deficiency@sh.itjust.works 1 point 1 year ago I know what an iterable is. But I am talking about Type[Iterable], which iirc does not obey falsey eval when empty. permalink fedilink source parent
[–] gravitas_deficiency@sh.itjust.works 1 point 1 year 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) permalink fedilink source parent
[–] BlackRoseAmongThorns@slrpnk.net 2 points 1 year 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. permalink fedilink source parent
[–] jj4211@lemmy.world 1 point 1 year ago else: # not my list, it is ourlist permalink fedilink source parent