how to check if a key is in between two another keys in a dictionary? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

how to check if a key is in between two another keys in a dictionary?

dict = {1:5, 5:6, 7:6, 9:8} if i have a task that if i get two integer let's say [4,8] i need to check that it is smaller to which keys in dict..keys() and need to insert a value(let's say '1') with key 4 and 8..and update or add the values of key 5 and 7(in b/w 4 and 8) by the new value...but for key 8, i need to insert a key value pair for that.. so i need a dict = {1:5, 5:7, 7:7, 8:1, 9:8} i am unable to check where the new key is smaller or greater than which of the keys which are already in the dict. help me to understand this, and share a code if you can..

11th Apr 2020, 6:00 PM
Amit Dubey
Amit Dubey - avatar
2 Answers
+ 4
I still have not fully understand what and how you will modify the dict. May be you can make your sample easier to understand. May be you can tell us a bit more what the keys and values are used for. Thanks! Some thoughts about 'inserting' new key-value pairs in the dict. You can not select the position where the new values should be placed, but after using dict.update(new_values) and a following sort, your new key will be at the desired place.
11th Apr 2020, 8:49 PM
Lothar
Lothar - avatar
0
11th Apr 2020, 7:13 PM
Amit Dubey
Amit Dubey - avatar