Jonathan E
Jonathan E

Reputation: 54

python for loop processed in strict order

I am using a python for loop to process json data, but I need to enforce certain data is processed first. What I want to happen is within my list, items where each_ADSL["VRF"] = 17 are processed before any others.

My json data i am interpreting looks something like this:

  "ADSL": [
{
  "CE_HOSTNAME": "TESTCE-DCNCE-01",
  "VRF": "19",
},
{
  "CE_HOSTNAME": "TESTCE-DCNCE-01",
  "VRF": "17",
}
]

I am interpreting this, then processing the data:

for each_ADSL in order["ADSL"]:
    do something

This needs to take into account numbers lower than 17 (so a simple sort won't work.) Can i turn order["ADSL"] into a list and sort it by criteria somehow?

Upvotes: 0

Views: 152

Answers (1)

Stan11
Stan11

Reputation: 284

How about something like this

myjson = {
    "ADSL": [
        {
            "CE_HOSTNAME": "TESTCE-DCNCE-01",
            "VRF": "19",
        },
        {
            "CE_HOSTNAME": "TESTCE-DCNCE-01",
            "VRF": "17",
        }
    ]
}


mylist = myjson["ADSL"]

list17 = []

for item in mylist:
    if item["VRF"] == "17":
        list17.append(item)

for item in list17:
    do_first_action()

for item in mylist:
    if item["VRF"] != "17":
        do_second_action()

Upvotes: 1

Related Questions