Retrieve an arbitrary element from a set without removing it
Hi, recently I wrote an algorithm, in which very often I had to get an arbitrary element from a set without removing it. Three possibilities came to mind: 1. x = some_set.pop() some_set.add(x) 2. for x in some_set: break 3. x = iter(some_set).next() Of course, the third should be the fastest. It nevertheless goes through all the iterator creation stuff, which costs some time. I wondered, why the builtin set does not provide a more direct and efficient way for retrieving some element without removing it. Is there any reason for this? I imagine something like x = some_set.get() or x = some_set.pop(False) and am thinking about providing a patch against setobject.c (preferring the .get() solution being a stripped down pop()). Before, I would like to know whether I have overlooked something or whether this can be done in an already existing way. Thanks, wr
participants (39)
-
"Martin v. Löwis"
-
A.M. Kuchling
-
Adam Olsen
-
Alex Martelli
-
Alexander Belopolsky
-
Antoine Pitrou
-
average
-
Ben Finney
-
Benjamin Peterson
-
Björn Lindqvist
-
Cameron Simpson
-
Chris Bergstresser
-
Daniel Stutzbach
-
Eric Smith
-
Florian Weimer
-
Georg Brandl
-
geremy condra
-
Greg Ewing
-
Guido van Rossum
-
James Y Knight
-
Jesse Noller
-
John Arbash Meinel
-
John Arbash Meinel
-
John Arbash Meinel
-
Mark Dickinson
-
Martin (gzlist)
-
Nick Coghlan
-
Oleg Broytman
-
Paul Moore
-
Raymond Hettinger
-
Scott David Daniels
-
ssteinerX@gmail.com
-
Stefan Behnel
-
Stephen J. Turnbull
-
Steven D'Aprano
-
Terry Reedy
-
Vitor Bosshard
-
Willi Richert
-
Yuvgoog Greenle