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

exclude values from a string or dictionar in python

I am consulting the databases of a mongo cluster and I need it to show me all the databases except some of them
this way i get the databases in dictionary form:

for db in myclient.list_databases():
    print(db)
``
`
and the output is as follows:
{'name': 'databasetest', 'sizeOnDisk': 32768.0, 'empty': False}
{'name': 'databaseQA', 'sizeOnDisk': 98304.0, 'empty': False}
{'name': 'databaseprod', 'sizeOnDisk': 32768.0, 'empty': False}
{'name': 'databasestage', 'sizeOnDisk': 32768.0, 'empty': False}
{'name': 'database2021', 'sizeOnDisk': 32768.0, 'empty': False}
{'name': 'database2022', 'sizeOnDisk': 32768.0, 'empty': False}



I would need the result to be all the db except:
{'name': 'database2021', 'sizeOnDisk': 32768.0, 'empty': False}
{'name': 'database2022', 'sizeOnDisk': 32768.0, 'empty': False}

how can i do it?

>Solution :

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

You can use a list comprehension like this. This will be creating a new list named filtered_dbs that doesn’t have the DB names you specified.

filtered_dbs = [db for db in myclient.list_databases() if db['name'] not in ['database2021', 'database2022']]

for db in filtered_dbs:
    print(db)
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