[Python-ideas] Allow deleting slice in an OrderedDict

Ram Rachum ram at rachum.com
Wed Dec 26 19:33:07 CET 2012


I agree with Terry that doing `del ordered_dict[:2]` is problematic because
there might be confusion between index numbers and dictionary keys.

My new proposed API: Build on `ItemsView` so we could do this: `del
ordered_dict.items()[:2]` and have it delete the first 2 items from the
ordered dict.


On Wed, Dec 26, 2012 at 6:58 PM, Guido van Rossum <guido at python.org> wrote:

> Perhaps the desired functionality can be spelled as a method? Would it be
> easy to implement?
>
> --Guido
>
>
> On Wednesday, December 26, 2012, Terry Reedy wrote:
>
>> On 12/25/2012 8:56 AM, Ram Rachum wrote:
>>
>>> When I have an OrderedDict, I want to be able to delete a slice of it. I
>>> want to be able to do:
>>>
>>>      del ordered_dict[:3]
>>>
>>> To delete the first 3 items, like I would do in a list.
>>>
>>> Is there any reason why this shouldn't be implemented?
>>>
>>
>> An OrderedDict is a mapping (has the mapping api) with a defined
>> iteration order (the order of entry). It is not a sequence and does not
>> have the sequence api. Indeed, a DictList is not possible because dl[2]
>> would look for the item associated with 2 as a key rather than 2 as a
>> position. So od[2:3] would *not* be the same as od[2], violating the
>> usually properly of within-sequence length-1 slices.
>>
>> --
>> Terry Jan Reedy
>>
>> ______________________________**_________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> http://mail.python.org/**mailman/listinfo/python-ideas<http://mail.python.org/mailman/listinfo/python-ideas>
>>
>
>
> --
> --Guido van Rossum (python.org/~guido)
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20121226/0251d05b/attachment.html>


More information about the Python-ideas mailing list