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

Removing specific elements from a list in Python

I have a list N. I want to remove elements mentioned in I from N. But there is an error. I present the expected output.

N=[i for i in range(1,11)]
I=[4,9]

N.remove(I)
print(N)

The error is

in <module>
    N.remove(I)

ValueError: list.remove(x): x not in list

The expected output is

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

[1,2,3,5,6,7,8,10]

>Solution :

You are trying to remove a list which is not in N. You either have to call .remove() for every element in I or you can do it like this:

I = [4, 9]
N = [i for i in range(1, 11) if i not in I]  # [1, 2, 3, 5, 6, 7, 8, 10]
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