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
#11
(Nov-27-2019, 10:23 AM)malevy Wrote: the keys are not ordered or following/
more spam like mess Wink **
not sure I understand. The order of the key is irrelevant. Ignore that they are sorted in my example. You didn't show sample data, so I had to invent
As to the order - as of python 3.7. dicts are order-preserving. i.e. the order in which keys are inserted/created in the dict is preserved. Before that they were unordered collection.
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
#12
what u did is amazing
im still trying to figure out the python code qoute thing :)
Reply
#13
the full picture is still not clear - e.g. would you repeat the process in which case different approach may be better, etc.
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
#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
#15
yesss!
thank you man
you solved it
genious
Reply
#16
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: spam[current] = current * 2
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
#17
@buran with regards to runtime considerations your solution is not optimal.
Searching through spam.values() is slower than through a normal list
In worst case, if value is last item in spam.values() you iterate twice over all items in dictionary.
Using my code you iterate only once even in worst case.
Reply
#18
@ThomasL, you are right. I didn't look at it from optimisation perspective
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 214 Dec-18-2025, 01:54 AM
Last Post: DavidSah
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 830 Nov-17-2025, 11:47 AM
Last Post: Winfried
Question Chain object that have parent child relation.. SpongeB0B 10 4,070 Dec-12-2023, 01:01 PM
Last Post: Gribouillis
  logging values from constantly updating CLI app imvirgo 1 1,841 Sep-27-2023, 05:01 PM
Last Post: deanhystad
  Updating Code And Having Issue With Keys Xileron 8 4,865 May-25-2023, 11:14 PM
Last Post: DigiGod
  dict class override: how access parent values? Andrey 1 3,324 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  Updating nested dict list keys tbaror 2 2,445 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Loop Dict with inconsistent Keys Personne 1 2,593 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Removing nan values from a dict tomtom 8 14,211 Oct-05-2021, 06:44 PM
Last Post: tomtom
  Create Dict from multiple Lists with duplicate Keys rhat398 10 6,888 Jun-26-2021, 11:12 AM
Last Post: Larz60+

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.