Created
December 20, 2023 14:39
-
-
Save JamesTheAwesomeDude/05f1bd33448f2a8ddc6e6bd7c0095ba2 to your computer and use it in GitHub Desktop.
get arbitrary element from set in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def peek1(collection): | |
"Get an arbitrary element from *collection*, non-destructively" | |
try: | |
return next(iter(collection)) | |
except StopIteration: | |
raise ValueError("axiom of choice on empty collection") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment