Python Forum

Full Version: updating certain values in dict. with relation to their keys
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi all
just joined
I have a very annoying problem
I have a dictionary containing key value pairs that are all integers.
I want to check if some values equal a number, and if so, the value of each of these items will be changed with relation to its key.
and if no value matches the current number a new item is created
relevant piece of code:


for key, value in mydict.items():
if value == current:
value = value + 3 * key
if current not in mydict:
mydict[current:current * 2]

thank you all
this does not work
I need the correct synthax...
If you want update value of the key then you should tell Python which key in which dictionary you would like to do so:

>>> d = dict(zip(range(1, 5), range(11, 15)))                                                                                                             
>>> d                                                                                                                                                    
{1: 11, 2: 12, 3: 13, 4: 14}
>>> for k, v in d.items(): 
...    if v == 13: 
...        d[k] = v + 3 * k 
...                                                                                                                                                      
>>> d                                                                                                                                                    
{1: 11, 2: 12, 3: 22, 4: 14}
thank you for the fast reply ,
.but this is exactly my problem
how can I access the item through its value
or do I have to create a new list of keys holding any value
there must be a better way
(Nov-27-2019, 09:38 AM)malevy Wrote: [ -> ].but this is exactly my problem
how can I access the item through its value

Nope, I don't understand what exactly is your problem. Can you specify?
if you need to access by value - you can create a new dict where unique values are keys and value is a list of respective keys from original dict.
For one time you can just loop over the items (i.e. key, values), check the value and take actions you want.

If you are more specific - it could help to show sample original dict and expected output, with transformation explained
looking at the first post
spam = {1:3, 4:6, 7:6, 9:10}
current = 6

if current in spam.values():
    for key, value in spam.items():
        spam[key] = value + 3 * key
else:
    current *= 2

print(spam)
print(current)
Output:
{1: 6, 4: 18, 7: 27, 9: 37} 6
maybe my question is actually this :
how can i get which keys hold this value
I need to fill the square brackets with the actual key
otherwise it will preform the function on all keys in d
am i right or do i not understand ?
(Nov-27-2019, 10:05 AM)malevy Wrote: [ -> ]maybe my question is actually this :
how can i get which keys hold this value

This is to answer your question about 'how can I get which keys holds this value(s)':

>>> d = dict(zip(range(1, 5), range(11, 15)))                                                                                                            
>>> values = [12, 14]                                                                                                                                    
>>> [k for k, v in d.items() if v in values]                                                                                                             
[2, 4]
>>> d.update({5: 12})                                                                                                                                    
>>> d                                                                                                                                                    
{1: 11, 2: 12, 3: 13, 4: 14, 5: 12}
>>> value = 12                                                                                                                                           
>>> [k for k, v in d.items() if v == value]                                                                                                              
[2, 5]
looking at the first post
spam = {1:3, 4:6, 7:6, 9:10}
current = 6

if current in spam.values():
    for key, value in spam.items():
        if value = current:
            spam[key] = value + 3 * key
else:
    current *= 2

print(spam)
print(current)
the keys are not ordered or following/
more spam like mess Wink **
Pages: 1 2