I have two lists:
mylist = ['total','age','gender','region','sex']
checklist = ['total','civic']
I have to work with some code I have inherited which looks like this:
for item in mylist:
if item in checklist:
do something:
How can I work with the code above to tell me that 'civic' is not in mylist?.
This would've been the ideal way to do it but I cant use it, don't ask me why.
for item in checklist:
if item not in mylist:
print item
Outcome:
civic
Paco
4,3722 gold badges28 silver badges52 bronze badges
asked Apr 3, 2014 at 9:50
Boosted_d16Boosted_d16
12.2k34 gold badges93 silver badges148 bronze badges
1
Your code should work, but you can also try:
if not item in mylist :
answered Dec 8, 2014 at 21:50
1
How about this?
for item in mylist:
if item in checklist:
pass
else:
# do something
print item
answered Apr 3, 2014 at 9:53
Santosh GhimireSantosh Ghimire
2,9898 gold badges33 silver badges58 bronze badges
2
if I got it right, you can try
for item in [x for x in checklist if x not in mylist]:
print [item]
answered Jun 8, 2018 at 21:50
Yury WalletYury Wallet
1,2541 gold badge12 silver badges21 bronze badges
You better do this syntax
if not [item in mylist]:
Code inside the if
answered Oct 13, 2016 at 17:44
1
ValueError: list.remove[x]: x not in list [Python] #
The Python "ValueError: list.remove[x]: x not in list" occurs when we call the remove[]
method with a value that does not exist in the list. To solve the error, check if the value exists in the list before removing it, or use a
try/except
block.
Here is an example of how the error occurs.
Copied!
my_list = ['apple', 'banana', 'kiwi'] # ⛔️ ValueError: list.remove[x]: x not in list my_list.remove['melon']
We passed a value that is not in the list to the remove[]
method which caused the error.
One way to solve the error is to check if the value is present in the list before passing it to the remove[]
method.
Copied!
my_list = ['apple', 'banana', 'kiwi'] if 'melon' in my_list: my_list.remove['melon'] print[my_list] else: # 👇️ this runs print['value is not in the list']
The in operator tests for membership. For example, x in l
evaluates to True
if x
is a member of l
, otherwise it evaluates to False
.
If you use a for
loop, make sure to iterate over a copy of the list if you need to remove any items.
Copied!
my_list = ['apple', 'banana', 'kiwi'] # ✅ iterate over copy for i in my_list.copy[]: my_list.remove[i] print[my_list] # 👉️ []
We used the copy[]
method to create a shallow copy of
the list when iterating.
This is necessary because mutating the list while iterating over it leads to confusing behavior.
The list.remove[] method removes the first item from the list whose value is equal to the passed in argument.
Copied!
my_list = ['a', 'b', 'c'] my_list.remove['a'] print[my_list] # 👉️ ['b', 'c']
The method raises a ValueError
if there is no such item.
The remove[]
method
mutates the original list and returns None
.
You can also use a try/except
statement to handle the error in case the value is not present in the list.
Copied!
my_list = ['a', 'b', 'c'] try: my_list.remove['r'] except ValueError: print['Item not in list'] print[my_list] # 👉️ ['a', 'b', 'c']
We call the remove[]
method on the list and if a ValueError
is raised, the except
block is run.
If you have a two-dimensional list, make sure you are calling the remove[]
method on the correct list.
Copied!
my_list = [['a', 'b'], ['c', 'd']] my_list[0].remove['b'] print[my_list] # 👉️ [['a'], ['c', 'd']]
We accessed the list item at index 0
and called the remove[]
method on it.
Had
we called the remove[]
method on the outer list, we would get a ValueError
because it doesn't contain the string "b"
.
Conclusion #
The Python "ValueError: list.remove[x]: x not in list" occurs when we call the remove[]
method with a value that does not exist in the list. To solve the error, check if the value exists in the list before removing
it, or use a try/except
block.