Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

delete a Item from Json File

I have a list of JSON file. I would like to remove a certain id. like I want to remove 3rd id and print get the json without this.
my json file:

data = [
          {
            "id": 1,
            "name": "John Smith",
            "email": "john@example.com"
          },
          {
            "id": 2,
            "name": "Jane Smith",
            "email": "jane@example.com"
          },
          {
            "id": 3,
            "name": "Bob Johnson",
            "email": "bob@example.com"
          }
        ]

The output I like

data = [
          {
            "id": 1,
            "name": "John Smith",
            "email": "john@example.com"
          },
          {
            "id": 2,
            "name": "Jane Smith",
            "email": "jane@example.com"
          }
        ]

I wrote this data = [item for item in data if item["id"] != 3] but I want a easy way to do it and I want to do it using index.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

For this can use del operator to remove a certain id by index:

del data[2]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading