Python Forum
updating certain values in dict. with relation to their keys
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
updating certain values in dict. with relation to their keys
#1
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...
Reply
#2
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}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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
Reply
#4
(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?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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 ?
Reply
#8
(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]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Chain object that have parent child relation.. SpongeB0B 10 1,205 Dec-12-2023, 01:01 PM
Last Post: Gribouillis
  logging values from constantly updating CLI app imvirgo 1 565 Sep-27-2023, 05:01 PM
Last Post: deanhystad
  Updating Code And Having Issue With Keys Xileron 8 1,274 May-25-2023, 11:14 PM
Last Post: DigiGod
  dict class override: how access parent values? Andrey 1 1,689 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  Updating nested dict list keys tbaror 2 1,314 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Loop Dict with inconsistent Keys Personne 1 1,635 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Removing nan values from a dict tomtom 8 7,243 Oct-05-2021, 06:44 PM
Last Post: tomtom
  Create Dict from multiple Lists with duplicate Keys rhat398 10 4,173 Jun-26-2021, 11:12 AM
Last Post: Larz60+
  Values not updating for restful call boomramada 0 1,679 Mar-13-2021, 01:08 PM
Last Post: boomramada
  Adding keys and values to a dictionary giladal 3 2,558 Nov-19-2020, 04:58 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020