Getting next value from key inside nested dict


My old code stored a and its assigned sACN universe in this form:

prev = {
  "312398": 1,
  "232312": 2,
  "312353": 3

Now im storing it as a nested dict with the additional attribute
device_name.model as follows:

current = {
  "312341": {
   "model": "Some-Model",
   "universe": 1
  "532342": {
    "model": "Some-other-model",
    "universe": 2

My old code had a function

def get_free_universe():
        return next(
           for i in range(1, 128)
           if i not in conf.values()

Called like this:

device_count = sdk.get_device_count() #setup Corsair devices config
for device_index in range(device_count):
    device_name = sdk.get_device_info(device_index)
    device_type = device_name.type
    if not in conf:
        if conf.keys == " ":
            print("config empty, universer = 1")
            universe = 1
            universe = get_free_universe()
        conf.update({ {"model": device_name.model, "universe": universe}}) 
        print(f"conf= {conf}")
        universe = conf[]

Now of course the part if i not in conf.values() doesnt work anymore. I need to look at every dict for its key universe. How can I achive that? Currently, since none of the values of the dicts with has an own value itself, so I cant get a next number and it just gives every the same universe (1).


One option can be to create a set of all the universe values in the new mapping. For example, with the mapping in the above example:

>>> universes = {v['universe'] for v in current.values()}
>>> universes
{1, 2}

Then you can use the new variable universes instead of conf.values() which you were doing above. Of course if you generate and assign a new universe, you also need to remember to add it to this hashset like so:

>>> universes.add(3)
>>> universes
>>> {1, 2, 3}

Then a slight modification is needed in the get_free_universe function:

def get_free_universe():

    # generate a new universe
    new_universe = next(
        for i in range(1, 128)
        if i not in universes
    # add to known universes
    return new_universe

Answered By – rv.kvetch

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published