简体   繁体   中英

Python dictionary - Remove dictionary from list of dictionaries

I have this dictionary:

 my_dict = {
  "camel_configuration": "rEIP.cccf",
  "test_environments": [
    {
      "Branch Coverage": "78/78(100%)",
      "Test Environment": "AB",
      "Statement Coverage": "73/73(100%)",
      "Test Status": "25",
      "MC/DC Coverage": "17/17(100%)"
    },
    {
      "Branch Coverage": "-",
      "Test Environment": "None",
      "Statement Coverage": "-",
      "Test Status": "36/36(100%)",
      "MC/DC Coverage": "-"
    }
  ]
}

How to remove a dictionary whose value of Test Environment is None? How to modify it so that I get this:

 my_dict = {
  "camel_configuration": "rEIP.cccf",
  "test_environments": [
    {
      "Branch Coverage": "78/78(100%)",
      "Test Environment": "AB",
      "Statement Coverage": "73/73(100%)",
      "Test Status": "25",
      "MC/DC Coverage": "17/17(100%)"
    }
]
}
my_dict = {
    "camel_configuration": "rEIP.cccf",
    "test_environments": [
        {
            "Branch Coverage": "78/78(100%)",
            "Test Environment": "AB",
            "Statement Coverage": "73/73(100%)",
            "Test Status": "25",
            "MC/DC Coverage": "17/17(100%)",
        },
        {
            "Branch Coverage": "-",
            "Test Environment": "None",
            "Statement Coverage": "-",
            "Test Status": "36/36(100%)",
            "MC/DC Coverage": "-",
        },
    ],
}
my_dict["test_environments"] = [
    test_environment
    for test_environment in my_dict["test_environments"]
    if test_environment["Test Environment"] != "None"
]
import pprint as pp

pp.pprint(my_dict, indent=2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM