Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swapping key-value pairs in a dictionary

I am looking for ways to swap all the key-value pairs of a given dictionary.

So far I could think of one way for doing it:

Ex:

>>>a = { 0: 'a', 1 : 'b', 2 : 'c' }
>>> {value : key for key,value in a.items()}
{'a': 0, 'b': 1, 'c' : 2}

But for this I would have to use extra space for declaring another dictionary.
I would like to know the methods could I use to swap the key-value pair more space-efficiency.

like image 580
Kshitij Saraogi Avatar asked Jul 13 '15 10:07

Kshitij Saraogi


People also ask

Can I swap keys and values in dictionary Python?

You can swap keys and values in a dictionary with dictionary comprehensions and the items() method.

How do I change the key values 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.

Can values in a dictionary be changed?

Change elements in a dictionaryWe can change the value of an item by accessing the key using square brackets ([]). To modify multiple values at once, we can use the . update() method, since this function overwrites existing keys.

How do you reverse a key-value pair in a dictionary?

3) Using loop and reversed() method In this method, you initialize a new empty dictionary for storing the reversed items of the original dictionary. Later, using the for loop you traverse through the original list and store every key-value pair in the new empty dictionary after reversing it using reversed() method.


1 Answers

But for this I would have to use extra space for declaring another dictionary.

Since a dictionary is essentially a lookup table, there is a concrete way it is layed out in memory; the keys are distributed efficiently and just point to values which—by themselves—have no special meaning. Thus, when you want to reverse the mapping, you can’t really use the existing structure; instead you will have to create new dictionary entries from scratch. The dictionary comprehension you have used in your question is a good and clear way to do that.

What you could do however is to reuse the dictionary you already have and add the new keys there (while removing the old ones):

for k in a:
    a[a[k]] = k
    del a[k]

This modifies the same dictionary, so it won’t have the (likely little) overhead of a new dictionary. Note that this assumes that all values are unique too so the mapping can be exactly reversed, and that the set of keys and values don’t share common values. Otherwise, you will run into dictionary size changed exceptions or missing values. You can avoid the former by creating a copy of the dictionary keys (although this means that you have a list to store now too):

for k in list(a):
    if a[k] != k:
        a[a[k]] = k
        del a[k]

A final note: It’s possible that modifying the dictionary multiple times like that might have some remapping side-effects though (to increase the hash table size etc.), but that’s possible implementation detail of CPython (and I’m not too sure about it).

like image 195
poke Avatar answered Oct 21 '22 00:10

poke