Python Forum
updating certain values in dict. with relation to their keys - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: updating certain values in dict. with relation to their keys (/thread-22785.html)

Pages: 1 2


updating certain values in dict. with relation to their keys - malevy - Nov-27-2019

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...


RE: updating certain values in dict. with relation to their keys - perfringo - Nov-27-2019

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}



RE: updating certain values in dict. with relation to their keys - malevy - Nov-27-2019

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


RE: updating certain values in dict. with relation to their keys - perfringo - Nov-27-2019

(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?


RE: updating certain values in dict. with relation to their keys - buran - Nov-27-2019

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


RE: updating certain values in dict. with relation to their keys - buran - Nov-27-2019

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



RE: updating certain values in dict. with relation to their keys - malevy - Nov-27-2019

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 ?


RE: updating certain values in dict. with relation to their keys - perfringo - Nov-27-2019

(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]



RE: updating certain values in dict. with relation to their keys - buran - Nov-27-2019

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)



RE: updating certain values in dict. with relation to their keys - malevy - Nov-27-2019

the keys are not ordered or following/
more spam like mess Wink **