How can we use 'not' operator to check if an item is present or not present in a list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can we use 'not' operator to check if an item is present or not present in a list?

Could any pls explain with an example..! nums = [1, 2, 3] print(not 4 in nums) print(4 not in nums) print(not 3 in nums) print(3 not in nums)

23rd Oct 2020, 2:40 PM
Sri Laya
Sri Laya - avatar
6 Answers
+ 3
This is a simple example. Explanation in code as comments. https://code.sololearn.com/czUbYGO9mNFI/?ref=app
23rd Oct 2020, 2:46 PM
Avinesh
Avinesh - avatar
+ 2
#Tag the language Pls... #Edit : Attunuru Baby Sri Laya #In python, lst = [1,2,3,4,5] inp = int(input()) if not inp in lst : #or if inp not in lst : print(inp, "not in list") else : print(inp, "present in list") #Inp in lst checks if input inp is in list, if yes return true otherwise false. Applying not reverse the process..
23rd Oct 2020, 2:42 PM
Jayakrishna 🇮🇳
+ 1
Attunuru Baby Sri Laya first you must check whether 'jamie' not in names, and it is true because that name is not there in the list. But then you do - not of true, which returns a false.
23rd Oct 2020, 2:53 PM
Avinesh
Avinesh - avatar
+ 1
Applying 'not' just reverse its boolean value.. not spam means if spam value true then it statement results false. And vise versa. But spam not is a incomplete statement.. Add what 8s next... The values 'none', ""( empty string), 0, false, empty list, all empty type values treated as false in boolean equalent.. Others comes to boolean value "true".. Edit : Attunuru Baby Sri Laya in my above example : Taking input value in to the variable inp. Ex : 5 or 8 In if :5 inp in lst : means 5 in [1,2,3,4,5] is true then not inp in lst returns false so else prints statements as the logic 5 is present in list. If inp=8 then (2nd way) inp not in lst : means 8 not in [1,2,3,4,5] returns true because 8 is not in list. So prints "8 is in not in list" Hope it clears....
23rd Oct 2020, 2:58 PM
Jayakrishna 🇮🇳
+ 1
I'm so greatful.. jayakrishna and avinesh😊
23rd Oct 2020, 3:00 PM
Sri Laya
Sri Laya - avatar
0
Avinesh i have a doubt in last line.! (not 'jamie' not in names) what is the difference between ' not jamie' vs 'jamie not'?
23rd Oct 2020, 2:52 PM
Sri Laya
Sri Laya - avatar