Another thing to consider is that the object passed in the items parameter may handle indices differently than a standard list (maybe it doesn't support negative indices, for example). In that case, checking that index is between -len(items) and len(items)-1 might lead to unexpected behavior.
The philosophy that dominates in Python is "It is easier to ask for forgiveness than permission". Many things in Python are implemented through exceptions, including every single iterator. They know when they've reached the end of the data because of a StopIteration exception being raised. They are designed to be performant enough that you can use them without too much worry, as long as the exception condition is still considerably less likely than the exception-free path. As always, it's recommended to use timeit to profile code if a section of code is performance-critical to determine what changes make a significant difference, but in general, a tight try and except block improves the reliability and sometimes the readability of code. Optimizations are being made all the time, and now Python 3.11's patch notes even claim that a try block that doesn't raise an exception is now "zero cost".
Same solution
This comment is hidden because it contains spoiler information about the solution
I love the simplicity. I need to get on this level of understanding code. Great stuff
I think this conditional is taking advantage of the fact that an empty array is "falsy" in python which is why this works :)
Didn't know about this If Array meaning that there's an array there. That's great.
Nice solution
Yes, @Invisibleman. Python uses
None
, rather than null though.If you find it let me know lol
yes, google 'python discord'
Another thing to consider is that the object passed in the
items
parameter may handle indices differently than a standard list (maybe it doesn't support negative indices, for example). In that case, checking thatindex
is between-len(items)
andlen(items)-1
might lead to unexpected behavior.i dont get it how this works, someone can reach me the topics?
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
The philosophy that dominates in Python is "It is easier to ask for forgiveness than permission". Many things in Python are implemented through exceptions, including every single iterator. They know when they've reached the end of the data because of a StopIteration exception being raised. They are designed to be performant enough that you can use them without too much worry, as long as the exception condition is still considerably less likely than the exception-free path. As always, it's recommended to use timeit to profile code if a section of code is performance-critical to determine what changes make a significant difference, but in general, a tight try and except block improves the reliability and sometimes the readability of code. Optimizations are being made all the time, and now Python 3.11's patch notes even claim that a try block that doesn't raise an exception is now "zero cost".
I don't think so.
Loading more items...