0

Working on a problem where I have to update a dictionary value.

For example, I have a dictionary with key value pairs that look like this:

{(0,0): some value, (5,0): some value, (12,3): some value, ...}

I am trying to use an if statement within a for loop that iterates through the 'names' of the key value pairs - (0,0), (5,0), (12,3), etc. - to see if the 'name' I'm looking for exists in the dictionary. If it does, I want to replace the value of that key with a different value.

Sort of new to python so if this seems confusing, or if there is an obvious solution you know why.

Kuldeep Singh Sidhu
  • 3,748
  • 2
  • 12
  • 22
  • 3
    The point of dicts is that you *don't* have to iterate to do a key lookup. – user2357112 Nov 21 '20 at 04:04
  • let's say the dictionary is `d = {(0,0): some value, (5,0): some value, (12,3): some value, ...}`, then you can check `if (0,0) in d:` and thats good enough. If it exists, you will get a True else False. – Joe Ferndz Nov 21 '20 at 04:05
  • But what i'm trying to do is check if the key exists in the dictionary, then if it does, replace the value of that key with a different value. Does that make sense? Getting a true or false value wouldn't do anything for me – codewander Nov 21 '20 at 04:08
  • Nevermind, I figured it out. Seems like the answer was pretty obvious in the end. Thanks for the help – codewander Nov 21 '20 at 04:09

5 Answers5

0

You can test whether key is in dictionary and replace with new value

def dict_replace(d, key, value):
    """Replace `key:value` only if key already in dictionary `d`"""
    if key in d:
        d[key] = value
tdelaney
  • 73,364
  • 6
  • 83
  • 116
-1
d = {(0,0): "some value", (5,0): "some value", (12,3): "some value", }

# change(5,0) to (50,0)

old = (5,0)
new = (50,0)

if old in d:
    d[new] = d[old]
    del d[old]
print(d)
{(0, 0): 'some value', (12, 3): 'some value', (50, 0): 'some value'}

PS: dict is used for value lookup using keys, here it is being used in wrong way!

Kuldeep Singh Sidhu
  • 3,748
  • 2
  • 12
  • 22
-1

Iterating over a dictionary actually just iterates over the keys. So,

my_dict = {(0,0): some value, (5,0): some value, (12,3): some value}
for k in my_dict:
    print(k)

will print all the keys((0, 0), (5, 0), (12, 3)) to the console. However, there is no need to iterate over the dictionary yourself because you can use the 'in' keyword. So,

if val in my_dictionary:
    print("value exists in dictionary")

will print that if val is one of the keys.

-2

The below is self explanatory. let me know if you have any questions Code:

dict1 = {(0,0): 'you', (5,0): 'are', (12,3): 'how'}
srch_key = (5,0)
val_to_replace = 'hello'
if dict1.get(srch_key):
    dict1[srch_key] = val_to_replace
print(dict1)
Aaj Kaal
  • 1,205
  • 1
  • 9
  • 8
  • What if the dict currently holds `srch_key: 0`? The `if` test false and the value would not be replaced. – tdelaney Nov 21 '20 at 04:21
  • srch_key will be a tuple and not 0. humble request - please re-read the qn – Aaj Kaal Nov 21 '20 at 04:29
  • OP says nothing of what "some value" is. You made up some strings... so what if the string is `""`? `dict1 = {(0,0): ''}` then `if dict1.get((0,0)): print("found")`. Remember that `dict.get` returns the found value and if that value is any falsy object like 0, None, "", etc... this code will incorrectly assume there was no key. `srch_key in dict1` is the normal and correct way to check whether a key is in a dictionary. I was quite specific in my original comment. – tdelaney Nov 21 '20 at 17:02
-2

I guess this could work:

d = {(0,0): "some value", (5,0): "some value", (12,3): "some value", }

value_im_looking_for=12,3

replace_value=12,0

for x in d:
    if x == value_im_looking_for:
        d[replace_value]=d.pop(value_im_looking_for)
        break

print(d)


>>> d = {(0,0): "some value", (5,0): "some value", (12,0): "some value", }

the .pop() method deletes the key it references and returns the value connected to that key, we can then feed that value into the new key.

groeterud
  • 117
  • 7