2009/10/23 Willi Richert <w.rich...@gmx.net>: > 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() >
I see this as being useful for frozensets as well, where you can't get an arbitrary element easily due to the obvious lack of .pop(). I ran into this recently, when I had a frozenset that I knew had 1 element (it was the difference between 2 other sets), but couldn't get to that element easily (get the pun?) _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com