
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How Does 'in' Operator Work on List in Python
The in operator in Python
In Python, the in operator determines whether a given value is a constituent element of a sequence such as a string, array, list, or tuple.
When used in a condition, the statement returns a Boolean result of True or False. The statement returns True if the specified value is found within the sequence. When it is not found, we get a False.
In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array. Square brackets are used to denote it, and a comma (,) is used to divide two things in the list.
Here we will see 3 different scenarios -
Assume we have taken a list containing random elements.
Example 1: Find One Item in Flat List
The following program checks whether the single element is present in the flat list or not using the in operator -
# input list lst = ["Hello", 10, "TutorialsPoint", 20, "python", "code"] # Checking if {TutorialsPoint} element in list using in operator print("TutorialsPoint" in lst) # Checking if {bigdata} element in list using in operator print("bigdata" in lst)
Output
When you run the program, it will show this output -
True False
As we can see from the output above, "TutorialsPoint" in the list evaluates to True. This indicates that the value "TutorialsPoint" can be found within the list. The term "bigdata" in the list evaluates to False. This means that the value "bigdata" was not found in the list.
Example 2: Find a List in a Nested List
The following program checks whether the given list is present in the nested list or not using the in operator:
# input list lst = [["Hello", 10], ["TutorialsPoint", 20], ["python", "code"]] # Checking if {TutorialsPoint,20} elements list present print(["TutorialsPoint",20] in lst) # Checking if {TutorialsPoint,code} elements list present print(["TutorialsPoint","code"] in lst)
Output
After running the program, you will get this result -
True False
As we can see from the output above, ["TutorialsPoint",20] in the list evaluates to True. This indicates that the list ["TutorialsPoint",20] can be found within the list.
Despite the fact that both "TutorialsPoint" and "code" elements are nested here, it returns False because they are not in the same lists. They appear in various lists of the nested list.
Example 3: Use of in with an if Statement
The following program checks whether a single element is present in the flat list or not using the in operator.
# input list lst = ["Hello", 10, "TutorialsPoint", 20, "python", "code"] # Checking if {TutorialsPoint} element in list using in operator if "TutorialsPoint" in lst: print('{TutorialsPoint} Element is in the given list') # Checking if {bigdata} element in list using in operator if "bigdata" in lst: print('{bigdata} Element is in the given list') # If {bigdata} is not in list else: print('{bigdata} Element is not present in the given list')
Output
This output will be displayed when the program runs:
{TutorialsPoint} Element is in the given list {bigdata} Element is not present in the given list