[Python-Dev] Retrieve an arbitrary element from a set without removing it

Nick Coghlan ncoghlan at gmail.com
Wed Oct 28 07:42:40 CET 2009

Alexander Belopolsky wrote:
> Odd indeed.  My first reaction was: it is not needed because lists
> support slicing, but when I tried to construct a list.get() using
> slicing the best I could come up with was the following hack
>>>> def lget(l, i, v):  return (l[i:] or [v])[0]
> ...
>>>> lget(range(10), 20, 200)
> 200
>>>> lget(range(10), 5, 50)
> 5
> Yet for some reason I never missed this functionality ...

People tend to do length checks on lists to decide which items are and
are not present. You can't do that with a dict.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list