RegularBlueJay
RegularBlueJay

Reputation: 13

How to fix 'ValueError: list.remove(x): x not in list' error in Python

I'm trying to remove the 'Maths, 76' from my list 'Marks' but it keeps throwing up:

line 2, in <module>
    Marks.remove("Maths, 76")
ValueError: list.remove(x): x not in list

I have tried changing the code to have Maths as a separate entity but it has not worked.

Marks = ['Amy', 'Jones', 'English', 72, 'Maths', 76, 'Computer Science', 96]
Marks.remove("Maths, 76")
print(Marks)

I believe the output should just be the list without the variable, but it just outputs the full list.

Upvotes: 0

Views: 15127

Answers (4)

Maryam Bahrami
Maryam Bahrami

Reputation: 1104

You face this error when the element does not exist in the list. I think the best way is to use if/else in a list comprehension.

For example:

Marks = ['Amy', 'Jones', 'English', 72, 'Maths', 76, 'Computer Science', 96]
Remove_result = [i for i in Marks if i != "Maths, 76"]
Remove_result
out[1].  ['Amy', 'Jones', 'English', 72, 'Maths', 76, 'Computer Science', 96]

Remove_result is not different from Marks, because "Maths, 76" does not exist in Marks

Note: You cannot use [Marks.remove(i) for i in Marks if i == "Maths, 76"], because it returns []. The reason non of iterations can meet the if condition.

Upvotes: 0

olinox14
olinox14

Reputation: 6663

You are trying to remove the string "Maths, 76" which does not exist in the list.

Either you do:

Marks.remove("Maths")
Marks.remove(76)

Either you change your list like this:

Marks = ['Amy', 'Jones', 'English', 72, "Maths, 76", 'Computer Science', 96]

NB: As far I understand what you are trying to do, you really should consider using dictionaries instead of lists for such a matter.

Example:

marks = {'first_name': 'Amy', 'name': 'Jones', 'English': 72, 'Maths': 76, 'Computer Science': 96}
del marks['Maths'] # to remove the Maths entry

Upvotes: 2

doctorlove
doctorlove

Reputation: 19272

Given

Marks = ['Amy', 'Jones', 'English', 72, 'Maths', 76, 'Computer Science', 96]
Marks.remove("Maths, 76")

you will get this error because the exact string "Maths, 76" is not in the list.

The string "Maths" is in the list and so is the number 76, so these can both be removed separately:

Marks.remove("Maths")
Marks.remove(76)

You could pair up the entries, assuming a first and second name, into tuples like this:

>>> list(zip(Marks[0::2], Marks[1::2]))
[('Amy', 'Jones'), ('English', 72), ('Maths', 76), ('Computer Science', 96)]

Then you could remove ('Maths', 76).

Or you can make a dictionary comprehension from your list:

>>> {k:v for k,v in zip(Marks[0::2], Marks[1::2])}
{'Amy': 'Jones', 'English': 72, 'Maths': 76, 'Computer Science': 96}

>>> lookup = {k:v for k,v in zip(Marks[0::2], Marks[1::2])}
>>> lookup['Maths']
76

The remove an item, use pop:

>>> lookup.pop('Maths')
76
>>> lookup
{'Amy': 'Jones', 'English': 72, 'Computer Science': 96}

Upvotes: 2

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

list.remove takes a single argument, which is the item you want to remove.

From the docs: https://docs.python.org/3/tutorial/datastructures.html

list.remove(x)
Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.

But "Maths, 76" is not an element in your list, hence you get the error ValueError: list.remove(x): x not in list
So you want to remove each element one at a time

Marks = ['Amy', 'Jones', 'English', 72, 'Maths', 76, 'Computer Science', 96]
Marks.remove("Maths")
Marks.remove(76)
print(Marks)

Or use a for loop

Marks = ['Amy', 'Jones', 'English', 72, 'Maths', 76, 'Computer Science', 96]

for item in ["Maths", 76]:
    Marks.remove(item)

print(Marks)

The output will be

['Amy', 'Jones', 'English', 72, 'Computer Science', 96]

Upvotes: 3

Related Questions