Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How does in operator work on list in Python?
The in operator in Python 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 False.
Syntax
element in list_name
Where element is the value to search for, and list_name is the list to search in.
Example 1: Find One Item in Flat List
The following program checks whether a single element is present in the flat list or not using the in operator −
# input list
fruits = ["apple", "banana", "orange", "mango", "grape"]
# Checking if banana is in the list
print("banana" in fruits)
# Checking if strawberry is in the list
print("strawberry" in fruits)
The output of the above code is −
True False
As we can see from the output above, "banana" in the list evaluates to True because the value "banana" is found within the list. The term "strawberry" evaluates to False because it 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 −
# nested list data = [["apple", 5], ["banana", 3], ["orange", 7]] # Checking if ["banana", 3] is present print(["banana", 3] in data) # Checking if ["apple", 7] is present print(["apple", 7] in data)
The output of the above code is −
True False
The list ["banana", 3] evaluates to True because this exact sublist exists in the nested list. However, ["apple", 7] returns False because while "apple" exists in the first sublist, it's paired with 5, not 7.
Example 3: Using in with if Statement
The following program demonstrates how to use the in operator with conditional statements −
# input list
colors = ["red", "blue", "green", "yellow", "purple"]
# Check if color exists and print appropriate message
color_to_find = "blue"
if color_to_find in colors:
print(f"'{color_to_find}' is in the color list")
else:
print(f"'{color_to_find}' is not in the color list")
# Check for a color that doesn't exist
missing_color = "pink"
if missing_color in colors:
print(f"'{missing_color}' is in the color list")
else:
print(f"'{missing_color}' is not in the color list")
The output of the above code is −
'blue' is in the color list 'pink' is not in the color list
Key Points
The
inoperator returnsTrueif the element is found,FalseotherwiseFor nested lists, it checks for exact sublists, not individual elements within sublists
The
inoperator is case-sensitive for stringsIt works with any iterable sequence like lists, tuples, and strings
Conclusion
The in operator provides a simple and readable way to check for membership in Python lists. It returns boolean values making it perfect for conditional statements and data validation.
