Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python dictionary replace values

I have a dictionary with 20 000 plus entries with at the moment simply the unique word and the number of times the word was used in the source text (Dante's Divine Comedy in Italian).

I would like to work through all entries replacing the value with an actual definition as I find them. Is there a simple way to iterate through the keywords that have as a value a number in order to replace (as I research the meaning)?

The dictionary starts:

{'corse': 378, 'cielo,': 209, 'mute;': 16, 'torre,': 11, 'corsa': 53, 'assessin': 21, 'corso': 417, 'Tolomea': 21}  # etc. 

Sort of an application that will suggest a keyword to research and define.

like image 816
user1478335 Avatar asked Nov 04 '13 17:11

user1478335


People also ask

Can you replace a value in a dictionary Python?

When working with dictionaries in Python, to replace a value in a dictionary, you can reassign a value to any key by accessing the item by key. In Python, dictionaries are a collection of key/value pairs separated by commas.

Can you modify the value in a dictionary?

Modifying a value in a dictionary is pretty similar to modifying an element in a list. You give the name of the dictionary and then the key in square brackets, and set that equal to the new value.

How do you replace a value in a list Python?

We can replace values inside the list using slicing. First, we find the index of variable that we want to replace and store it in variable 'i'. Then, we replace that item with a new value using list slicing.

How do you update a dictionary with multiple values?

In Python, we can add multiple key-value pairs to an existing dictionary. This is achieved by using the update() method. This method takes an argument of type dict or any iterable that has the length of two - like ((key1, value1),) , and updates the dictionary with new key-value pairs.


2 Answers

You cannot select on specific values (or types of values). You'd either make a reverse index (map numbers back to (lists of) keys) or you have to loop through all values every time.

If you are processing numbers in arbitrary order anyway, you may as well loop through all items:

for key, value in inputdict.items():     # do something with value     inputdict[key] = newvalue 

otherwise I'd go with the reverse index:

from collections import defaultdict  reverse = defaultdict(list) for key, value in inputdict.items():     reverse[value].append(key) 

Now you can look up keys by value:

for key in reverse[value]:     inputdict[key] = newvalue 
like image 61
Martijn Pieters Avatar answered Sep 20 '22 17:09

Martijn Pieters


via dict.update() function

In case you need a declarative solution, you can use dict.update() to change values in a dict.

Either like this:

my_dict.update({'key1': 'value1', 'key2': 'value2'}) 

or like this:

my_dict.update(key1='value1', key2='value2') 

via dictionary unpacking

Since Python 3.5 you can also use dictionary unpacking for this:

my_dict = { **my_dict, 'key1': 'value1', 'key2': 'value2'} 

Note: This creates a new dictionary.

via merge operator or update operator

Since Python 3.9 you can also use the merge operator on dictionaries:

my_dict = my_dict | {'key1': 'value1', 'key2': 'value2'} 

Note: This creates a new dictionary.

Or you can use the update operator:

my_dict |= {'key1': 'value1', 'key2': 'value2'} 
like image 35
Rotareti Avatar answered Sep 20 '22 17:09

Rotareti