Home > Back-end >  dictionary loop manipulation
dictionary loop manipulation

Time:06-21

I have the following dict:

a = {'chance': {'instance1': '', 'instance2': '', 'instance3': '', 'instance4': ''}}

, I want to be able to return like this with loops, while loading from a list:

{'chance': {'instance1': '1', 'instance2': '2', 'instance3': '3', 'instance4': '4'}}

the list looks like this:

pol=["1","2","3","4"]

I tried doing it with loops like this:

for i in range(1,5):
        for j in range(4):
            print(j)

or like this:

 for  line in inst['chance']:
        miroute = re.match('instance*', line)
        if miroute:
            for i in range(4):
                print(i)
                break
            print(miroute)

but with no success, do you have any idea how to process it

CodePudding user response:

You're on the right track, walking through dictionaries can be tricky, but I highly recommend f-strings if you are trying to match up a lot of numbers and strings together:

a = {'chance': {}}

for i in range(1,5):
    a['chance'][f'instance{i}'] = i

print(a)
{'chance': {'instance1': 1, 'instance2': 2, 'instance3': 3, 'instance4': 4}}

If you absolutely must start with a as you have above, you can still accomplish your with the same code, as it will still write the keys in the same manner:

a = {'chance': {'instance1': '', 'instance2': '', 'instance3': '', 'instance4': ''}}

for i in range(1,5):
    a['chance'][f'instance{i}'] = i

print(a)
{'chance': {'instance1': 1, 'instance2': 2, 'instance3': 3, 'instance4': 4}}

CodePudding user response:

One way is this:

a = {'chance': {'instance1': '', 'instance2': '', 'instance3': '', 'instance4': ''}}
pol=["1","2","3","4"]
d = a['chance']
keys = iter(d.keys())
for p in pol:
    d[next(keys)] = p
print(a)

Output:

{'chance': {'instance1': '1', 'instance2': '2', 'instance3': '3', 'instance4': '4'}}

Some help in the docs:

CodePudding user response:

Here's yet another way...

a = {'chance': {'instance1': '', 'instance2': '', 'instance3': '', 'instance4': ''}}
pol=["1","2","3","4"]

b = {'chance': {}}
for x, y in zip(a['chance'], pol):
    b['chance'][x] = y

print(b)

{'chance': {'instance1': '1', 'instance2': '2', 'instance3': '3', 'instance4': '4'}}

CodePudding user response:

Easy Way.

dict_ = {'chance': {'instance1': '', 'instance2': '', 'instance3': '', 'instance4': ''}}

pol=["1","2","3","4"]
for index,key in enumerate(list(dict_['chance'])):
    dict_['chance'][key]=pol[index]

print(dict_)

OUTPUT

{'chance': {'instance1': '1', 'instance2': '2', 'instance3': '3', 'instance4': '4'}}

CodePudding user response:

you could simply use dict comprehension in the a["chance"]

a = {'chance': {'instance1': '', 'instance2': '', 'instance3': '', 'instance4': ''}}
a["chance"].update({x:str(i) for i,(x,_) in enumerate(a["chance"].items())})
print(a)

output:

{'chance': {'instance1': '0', 'instance2': '1', 'instance3': '2', 'instance4': '3'}}

CodePudding user response:

You can expand the below solution for multiple 'chance' if you like:

>>> a = {'chance1': {'instance1': '', 'instance2': '', 'instance3': '', 'instance4': ''},
     'chance2': {'instance5': '', 'instance6': '', 'instance7': '', 'instance8': ''},}
>>> pol=["1","2","3","4", "5", "6", "7","8"]
>>> {key : {k : pol[f_idx*4 idx] for idx, (k,v) in enumerate(a[key].items())} for f_idx, (key, dct) in enumerate(a.items())}
{'chance1': {'instance1': '1','instance2': '2','instance3': '3','instance4': '4'},
 'chance2': {'instance5': '5','instance6': '6','instance7': '7','instance8': '8'}}

You can do this as one_line:

>>> a['chance'] = {k : pol[idx] for idx, (k,v) in enumerate(a['chance'].items())}
>>> a
{'chance': {'instance1': '1','instance2': '2','instance3': '3','instance4': '4'}}

Input:

a = {'chance': {'instance1': '', 'instance2': '', 'instance3': '', 'instance4': ''}}
pol=["1","2","3","4"]
  • Related