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
#14
Hi malevy, this is how i understand what you want to do. Is that right?
dictionary = {1: 10, 2: 20, 3: 30, 4:40}

current = 10
found = False

for key, value in dictionary.items():
    if value == current:
        dictionary[key] = value + 3 * key
        found = True

if not found:
    dictionary[current] = current * 2
Reply


Messages In This Thread
RE: updating certain values in dict. with relation to their keys - by ThomasL - Nov-27-2019, 01:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Chain object that have parent child relation.. SpongeB0B 10 2,990 Dec-12-2023, 01:01 PM
Last Post: Gribouillis
  logging values from constantly updating CLI app imvirgo 1 1,361 Sep-27-2023, 05:01 PM
Last Post: deanhystad
  Updating Code And Having Issue With Keys Xileron 8 3,099 May-25-2023, 11:14 PM
Last Post: DigiGod
  dict class override: how access parent values? Andrey 1 2,503 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  Updating nested dict list keys tbaror 2 1,916 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Loop Dict with inconsistent Keys Personne 1 2,184 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Removing nan values from a dict tomtom 8 11,499 Oct-05-2021, 06:44 PM
Last Post: tomtom
  Create Dict from multiple Lists with duplicate Keys rhat398 10 5,710 Jun-26-2021, 11:12 AM
Last Post: Larz60+
  Values not updating for restful call boomramada 0 2,060 Mar-13-2021, 01:08 PM
Last Post: boomramada
  Adding keys and values to a dictionary giladal 3 3,439 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