Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change key in OrderedDict without losing order

Starting with

OrderedDict([('a', 1), ('c', 3), ('b', 2)]) 

is it possible to end up with

OrderedDict([('a', 1), ('__C__', 3), ('b', 2)]) 

making sure that the '__C__' item is before 'b' and after 'a' i.e. keeping order?

like image 825
frnhr Avatar asked Aug 27 '12 23:08

frnhr


People also ask

Does dict keys preserve order?

It's a dictionary subclass specially designed to remember the order of items, which is defined by the insertion order of keys. This changed in Python 3.6. The built-in dict class now keeps its items ordered as well.

How do I change the order of an ordered dictionary?

Reordering. It is possible to change the order of the keys in an OrderedDict by moving them to either the beginning or the end of the sequence using move_to_end() . The last argument tells move_to_end() whether to move the item to be the last item in the key sequence (when True ) or the first (when False ).

Do dict Keys return same order?

No, there is no guaranteed order for the list of keys returned by the keys() function. In most cases, the key list is returned in the same order as the insertion, however, that behavior is NOT guaranteed and should not be depended on by your program.

Can you modify the keys in a dictionary?

Since keys are what dictionaries use to lookup values, you can't really change them. The closest thing you can do is to save the value associated with the old key, delete it, then add a new entry with the replacement key and the saved value.

What happens when you change the key value in ordereddict?

Key value Change: If the value of a certain key is changed, the position of the key remains unchanged in OrderedDict. 2. Deletion and Re-Inserting: Deleting and re-inserting the same key will push it to the back as OrderedDict, however, maintains the order of insertion.

How to reorder a key-value pair in ordereddict in Python?

To reorder a key-value pair to the front or the end of an OrderedDict in python, we have the move_to_end() method. Look, this is ‘d’: Now, let’s move (‘e’,5) to the end. This method takes a key as an argument. But it may also take another argument- ‘last’. This may take one of two values: True or False.

What is ordereddict in Python?

An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted. The only difference between dict () and OrderedDict () is that: OrderedDict preserves the order in which the keys are inserted. A regular dict doesn’t track the insertion order, and iterating it gives the values in an arbitrary order.

How do you pop items from an ordereddict?

This works by iterating over the whole OrderedDict(using its length), and pop'ing its first item(by passing Falseto .popitem(): the default of this method is to pop the last item) into kand v(respectively standing for keyand value); and then inserting this key/value pair, or the new key with its original value, at the end of the OrderedDict.


2 Answers

You could try:

>>> d = OrderedDict([('a', 1), ('c', 3), ('b', 2)]) >>> d OrderedDict([('a', 1), ('c', 3), ('b', 2)]) >>> d2 = OrderedDict([('__C__', v) if k == 'c' else (k, v) for k, v in d.items()]) >>> d2 OrderedDict([('a', 1), ('__C__', 3), ('b', 2)]) 
like image 154
MRAB Avatar answered Sep 22 '22 06:09

MRAB


if you wish to mutate the current dictionary object:

def change_key(self, old, new):     for _ in range(len(self)):         k, v = self.popitem(False)         self[new if old == k else k] = v 

This works by iterating over the whole OrderedDict (using its length), and pop'ing its first item (by passing False to .popitem(): the default of this method is to pop the last item) into k and v (respectively standing for key and value); and then inserting this key/value pair, or the new key with its original value, at the end of the OrderedDict.

By repeating this logic for the entire size of the dict, it effectively rotates the dict completely, thus recreating the original order.

like image 41
fbstj Avatar answered Sep 21 '22 06:09

fbstj