[Python-ideas] data structures should have an .any() method

Gerald Britton gerald.britton at gmail.com
Fri Sep 4 15:47:54 CEST 2009


Though IIRC the OP doesn't want to delete the item, so you would need
something like:

key, data = dict.popitem(); dict[key] = data


On Fri, Sep 4, 2009 at 9:35 AM, ilya<ilya.nikokoshev at gmail.com> wrote:
> I think dict.popitem() does something close to what the original post wanted.
>
> http://docs.python.org/3.1/library/stdtypes.html#dict.popitem
>
> On Fri, Sep 4, 2009 at 4:17 PM, Nick Coghlan<ncoghlan at gmail.com> wrote:
>> Matteo Dell'Amico wrote:
>>>> The shorthand expression above also suffers from the obscurity that
>>>> Stefan was complaining about - there is very little to hint that
>>>> "next(iter(obj))" means "get an arbitrary object out of a container".
>>>> The StopIteration exception this approach will throw for an empty
>>>> container is also rather unhelpful.
>>>
>>> Why? next(iter(obj)) means, pretty explicitly to me, "iterate on obj and
>>> give me one element".
>>
>> Because it overspecifies the semantics of what you're trying to do. It
>> just happens that when the requirement is "get me any object in this
>> container" the design of Python means that the easiest implementation is
>> "get me the first object in this container".
>>
>> The expression form then reflects the implementation rather than the
>> algorithmic intent.
>>
>> That said, this concise way of implementing the desired feature is
>> certainly one of the reasons I am -0 on the idea of adding it to the
>> standard library.
>>
>> Cheers,
>> Nick.
>>
>> --
>> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
>> ---------------------------------------------------------------
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> http://mail.python.org/mailman/listinfo/python-ideas
>>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



-- 
Gerald Britton



More information about the Python-ideas mailing list