Using list comprehension:
print [x for x in item if x not in Z]
or using filter function :
filter[lambda x: x not in Z, item]
Using set
in any form may create a bug if the list being checked contains non-unique elements, e.g.:
print item
Out[39]: [0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print Z
Out[40]: [3, 4, 5, 6]
set[item] - set[Z]
Out[41]: {0, 1, 2, 7, 8, 9}
vs list comprehension as above
print [x for x in item if x not in Z]
Out[38]: [0, 1, 1, 2, 7, 8, 9]
or filter function:
filter[lambda x: x not in Z, item]
Out[38]: [0, 1, 1, 2, 7, 8, 9]
Created: February-07, 2021 | Updated: February-28, 2021 In this tutorial, we will introduce how to check if an element is not in a list in Python.not in
to Check if an Element Is Not in a List in Python__contains__
Method of the List to Check if an Element Is Not in a List
in Python
Use not in
to Check if an Element Is Not in a List in Python
The in
keyword in Python can be used to check whether an element is present in a collection or not. If an element is present, then it returns True
; otherwise, it returns False
. For
example:
x = 3 in [1,2,5]
y = 1 in [1,2,5]
print[x]
print[y]
Output:
False
True
If we need to check if an element is not in the list, we can use the not in
keyword. The not
is a logical operator to converts True
to False
and vice-versa. So if an element is not present in a list, it will return True
.
x = 3 not in [1,2,5]
print[x]
Output:
True
Use the __contains__
Method of the List to Check if an Element Is Not in a List in Python
In Python, we have magic functions that are associated with classes and are not to be meant invoked directly although it is possible. One such function called __contains__
can be used to check if an element is present in a list or not. For example,
x = [1,2,5].__contains__[1]
print[x]
x = [1,2,5].__contains__[3]
print[x]
Output:
True
False
Although this method works, it is not advisable to use this method since magic functions are not intended to be invoked directly.
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.
Related Article - Python List
In this article, we will learn about various types of list methods available to us in Python 3.x. Or earlier. These operators allow us to perform the basic operations on the list content.
In & Not in operators
“in” operator − This operator is used to check whether an element is present in the passed list or not. Returns true if the element is present in the list otherwise returns false.
“not in” operator − This operator is used to check whether an element is not present in the passed list or not. Returns true if the element is not present in the list otherwise returns false. Let’s take a look at the example to understand it better.
Example
lis = ['t','u','t','o','r','i','a','l'] # in operator if 't' in lis: print ["List is having element with value t"] else : print ["List is not having element with value t"] # not in operator if 'p' not in lis: print ["List is not having element with value p"] else : print ["List is having element with value p"]
Output
List is having element with value t List is not having element with value p
Len[] , Max[] & Min[] operators
len[] − This function returns the length of list. If the object has no asssociation with length , error is raised .
min[] − This function returns the minimum element from the list.
max[] − This function returns the maximum element from the list.
Let’s take a look at the example to understand it better.
Example
lis = ['t','u','t','o','r','i','a','l'] # len[] print ["The length of list is : ", end=""] print [len[lis]] # min[] print ["The minimum element of list is : ", end=""] print [min[lis]] # max[] print ["The maximum element of list is : ", end=""] print [max[lis]]
Output
The length of list is : 8 The minimum element of list is : a The maximum element of list is : u
Conclusion
In this article, we learnt how to implement in and not in operators along with len[], max[], & min[] method.
Updated on 01-Jul-2020 07:19:09
- Related Questions & Answers
- Find Min-Max in heterogeneous list in Python
- max[] and min[] in Python
- Dictionary Methods in Python [cmp[], len[], items[]…]
- Use of min[] and max[] in Python
- Min-Max Heaps
- Python – Test if elements of list are in Min/Max range from other list
- Program to fill Min-max game tree in Python
- Symmetric Min-Max Heaps
- Custom len[] Function In Python
- List Methods in Python?
- Sorting max to min value in MySQL
- Min-Max Range Queries in Array in C++
- Explain the use of MIN[] and MAX[] using MySQL in Python?
- Convert min Heap to max Heap in C++
- Perform min/max with MongoDB aggregation