Home > front end >  Is there a way to take a key from one dictionary and add it to another?
Is there a way to take a key from one dictionary and add it to another?

Time:02-04

My goal is to take a key from one dictionary which holds lots of keys and values, and add the key to an empty dictionary. The key is stored in a variable name called "bossname" all of the keys in that dictionary have the same variable name so that I can use it in other places. The value to all of the keys in the boss_list dictionary is stored in "min"

boss_list = {
"test": 20,
"hi": 3,
"170": 80,
}

boss_timers_dict = {}

    async def boss_down(bossname, min):
    due_at = int(time.time() (min))
    hm_from_now = int(due_at) - int(time.time())
    due_in_six = int(due_at) - int(time.time()   int(6))
    print("timed msg here...")

    for hm_from_now in range(due_at):

        if hm_from_now == min:
            #This is where my question is about
            if bossname in boss_list:
                boss_timers_dict.update([bossname])

            coro = asyncio.sleep(1)
            await coro
            del boss_timers_dict[bossname]
            break

What I expected to happen was for the "bossname" to be taken from the boss_list dictionary and added to the empty boss_timers_dict dictionary. I have tried doing

if bossname in boss_list:
    boss_timers_dict.update(bossname)

CodePudding user response:

If you want to assign the contents of bossname to boss_timers_dict, just do:

if bossname in boss_list:
    boss_timers_dict = boss_list[bossname]

Suppose the boss_list dictionary have this structure, and the bossman variable contains the string "bossman":

{'bossname': {k1: i1, k2: i2, ....}, key2: value 2}

By doing that above, the boss_timers_dict should now contain:

{k1: i1, k2: i2, ....}

But if you want the new dict to have the key bossname that containing the boss_list['bossname'], just do :

if 'bossname' in boss_list:
    boss_timers_dict['bossname'] = boss_list['bossname']

Therefore, the boss_timers_dict should now contain

{'bossname': {k1: i1, k2: i2, ....}}
  • Related