Skip to main content
16 events
when toggle format what by license comment
Nov 23, 2023 at 20:28 comment added Puff I guess I'm just biased by my most comon usecase.
Nov 22, 2023 at 16:31 comment added Caridorc @Puff but why test all if we only care about the first? Also vectorized use case is much more specialized
Nov 21, 2023 at 20:19 comment added Puff I'd argue a better default value for condition is Bool. I think it will reduce overhead in the cases where the iterable is an array that supports vectorized functions. In that case, the user can test for the condition on the whole array at once and pass that to the function. Depending on when the condition is fulfilled and the size of the array, that may be preferred.
Apr 11, 2021 at 23:31 comment added brandonscript Yep! The other thing you'll want to do here is test whether condition exists before trying to write a condition; sometimes even returning x at all from an iterator could be expensive (e.g., if x is a generator yield). So check if condition otherwise just return next(x for x in iterable, default)
Apr 11, 2021 at 23:28 comment added Caridorc @brandonscript interesting I did not know about that
Apr 10, 2021 at 4:39 comment added brandonscript why would you not just use the built-in default functionality of next()? next(x for x in iterable if condition(x), default)
Mar 9, 2020 at 21:23 comment added Caridorc @Zorf I added an alternative version with a default argument
Mar 9, 2020 at 21:22 history edited Caridorc CC BY-SA 4.0
added version with default argument
Feb 26, 2020 at 22:39 comment added Zorf There should be an optional default argument, and if that argument not be supplied, only then raise an exception when no element in the sequence satisfy the condition.
May 25, 2018 at 12:45 comment added Guy Baldrickk I feel like this isn't an iteration method. You won't call this one in a contest of an iterator. But I'm not feeling too strongly about it :)
May 24, 2018 at 7:51 comment added Baldrickk @guyarad StopIteration is the canonical "out of elements" exception in python. I don't see a problem with it being thrown. I'd probably use a default of "None" which can be passed in as a default parameter to the function.
Feb 5, 2018 at 8:25 comment added Guy no... I just "invented" an exception. If you are implementing a first function, having it raise a StopIteration exception just seems weird. Anyway, take a look at boltons.iterutils.first.
Jan 28, 2018 at 11:22 comment added Caridorc @guyarad Is that a kind of ValueError?
Jan 26, 2018 at 3:28 comment added Guy If you are wrapping it with a method, at least catch StopIteration and raise EmptySequence error. Would be much prettier when there are no elements.
Feb 19, 2016 at 19:31 history edited Caridorc CC BY-SA 3.0
added exceptional scenario description and test
Feb 19, 2016 at 19:25 history answered Caridorc CC BY-SA 3.0