How to preserve the dict order while replacing the value of ke

117 Views Asked by At
Envs = ["re","pr","qa"]
    j = {'e.ticket.instructions': 'text', 'AGroup.ENV': 'text, loop','create.ENV.cro': 'boolean, loop'}
    for env in Envs:
        j = {key.replace('ENV', env): j.pop(key) for key in j.keys()}
        for i in j:
            if "boolean" in j[i]:
                if env == "pr" and i == 'e.ticket.instructions':
                    print "ignore"
                else:
                    print "proceed"
               
                print(k)
            else
                print "not boolean"
        j = {key.replace(env, 'ENV'): j.pop(key) for key in j.keys()}

Expected Output: I should be able to replace env and iterate through keys and values in the dict but preserve the order as original order below.

j = {'e.ticket.instructions': 'text', 'AGroup.re': 'text, loop', 'create.pr.cr': 'boolean, loop'}

Also if env == "pr", it should ignore 'e.ticket.instructions' key, so the j should like below:

j = {'e.ticket.instructions': 'text', 'AGroup.pr': 'text, loop', 'create.pr.cr': 'boolean, loop'}

but its not preserving the order and changing it to below:

j = {'create.pr.cr': 'boolean, loop', 'AGroup.pr': 'text, loop', 'e.ticket.instructions': 'text'}
 

How to preservce the order while replacing the word in key.

1

There are 1 best solutions below

0
Joffan On

What you are doing is clearly based on some odd code, which is getting odder as you try to do more with it. You should start your question with a description of what you are actually trying to achieve, which help in two ways: people may be better able to help you, and you yourself will perhaps gain some clarity on the task at hand.

If, as seems possible, you are trying to make combinations of keys based on the possible substitutions of the env value into the template dictionary j, you can achieve this in an easy-to-understand manner by:

    Envs = ["re","pr","qa"]
    j_template = {'e.ticket.instructions': 'text', 'AGroup.ENV': 'text, loop','create.ENV.cro': 'boolean, loop'}
    j_new = dict()           # make a new dictionary for substituted keys
    for env in Envs:
        for ky, val in j_template.items():
            # add updated item to interpreted dictionary
            j_new[ky.replace('ENV', env)] = val

If the tests/printing is important, you can update accordingly. Final value of j_new from this is:

{'e.ticket.instructions': 'text', 'AGroup.re': 'text, loop', 'create.re.cro': 'boolean, loop', 'AGroup.pr': 'text, loop', 'create.pr.cro': 'boolean, loop', 'AGroup.qa': 'text, loop', 'create.qa.cro': 'boolean, loop'}

Note that although we specify j_new['e.ticket.instructions'] three times (since the text substitution does nothing), the interpreted dictionary will only have one entry.

As observed in comments, order is generally not something that is primary to usage of the dictionary data structure. If ordering is really important (or useful), some other structure (such as a list of tuples) may be more suitable. The order of items in a dictionary as being insertion order was only guaranteed from Python 3.7 onwards, but you are using Python 2.x (and it's a good idea to move over to Python 3.x).