Allow deleting slice in an OrderedDict

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? Thanks, Ram.

(replying again, as the original somehow had a broken googlegroups.com address instead of the proper python.org one) On Tue, Dec 25, 2012 at 11:56 PM, Ram Rachum <ram.rachum@gmail.com> wrote:
Yes, because if you want to do that, you need a list, not an ordered dictionary. Don't try to lump every possible operation into one incoherent uber-type. If you need mutable list-like behaviour *and* mapping behaviour, you're better off with an ordinary mapping and a separate list of keys. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On 12/25/2012 8:56 AM, Ram Rachum wrote:
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

On 26.12.12 18:58, Guido van Rossum wrote:
Perhaps the desired functionality can be spelled as a method? Would it be easy to implement?
This is a pretty trivial method. def drop_items(self, n, last=True) for i in range(n): self.popitem(last) You can wrap it with "try/except KeyError" or add pre-execution checks if you will. I doubt such trivial and not common used method needed to be in stdlib.

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@python.org> wrote:

(replying again, as the original somehow had a broken googlegroups.com address instead of the proper python.org one) On Tue, Dec 25, 2012 at 11:56 PM, Ram Rachum <ram.rachum@gmail.com> wrote:
Yes, because if you want to do that, you need a list, not an ordered dictionary. Don't try to lump every possible operation into one incoherent uber-type. If you need mutable list-like behaviour *and* mapping behaviour, you're better off with an ordinary mapping and a separate list of keys. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On 12/25/2012 8:56 AM, Ram Rachum wrote:
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

On 26.12.12 18:58, Guido van Rossum wrote:
Perhaps the desired functionality can be spelled as a method? Would it be easy to implement?
This is a pretty trivial method. def drop_items(self, n, last=True) for i in range(n): self.popitem(last) You can wrap it with "try/except KeyError" or add pre-execution checks if you will. I doubt such trivial and not common used method needed to be in stdlib.

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@python.org> wrote:
participants (8)
-
alex23
-
Guido van Rossum
-
MRAB
-
Nick Coghlan
-
Ram Rachum
-
Ram Rachum
-
Serhiy Storchaka
-
Terry Reedy