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

Dropping specific elements from lists

I have data stored in the list. Below you can see my data.

listName = ['column1','column2','column3','column4']

Now I want to drop elements with titles 'column2' and 'column3'

I tried this command but is not work.

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

listName=listName.drop(['column2','column3'],axis=1)

Can anybody help me how to solve this problem?

>Solution :

If you need to remove them by value, I would create a list of elements to remove and filter the former list via list-comprehension:

to_remove = ['column2','column3']

filtered = [x for x in listName if x not in to_remove]

Returning:

['column1','column4']
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