Skip to content

Instantly share code, notes, and snippets.

@JamesTheAwesomeDude
Created December 20, 2023 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesTheAwesomeDude/05f1bd33448f2a8ddc6e6bd7c0095ba2 to your computer and use it in GitHub Desktop.
Save JamesTheAwesomeDude/05f1bd33448f2a8ddc6e6bd7c0095ba2 to your computer and use it in GitHub Desktop.
get arbitrary element from set in Python
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