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.
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
envvalue into the template dictionaryj, you can achieve this in an easy-to-understand manner by:If the tests/printing is important, you can update accordingly. Final value of j_new from this is:
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).