How does in operator work on list in Python?


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

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 this article, we will show how the in operator works on a list in different ways using Python. Here we will see 3 different scenarios −

  • To find a single element/object in a single list/flat list.

  • To find multiple elements list in a nested list.

  • Usage of in operator with if statement.

Assume we have taken a list containing random elements.

Method 1: To find a single element/object in a single list/flat list

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list and give it some random values.

  • Check if the element is present in list or not using in operator and print it.

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

On executing, the above program will generate the following output −

True
False

To begin, we have filled a list lst with random values. The in operator is then used to determine whether or not some values are part of the previous sequence.

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.

Method 2: To find multiple elements list in a nested

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input nested list and give it some random list values.

  • Check if the list is present in the nested list or not using in operator and print it.

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 in nested list using in operator print(["TutorialsPoint",20] in lst) # Checking if {TutorialsPoint,code} elements list present in nested list using in operator print(["TutorialsPoint","code"] in lst)

Output

On executing, the above program will generate the following output −

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.

The list ["TutorialsPoint", "code"] in the list evaluates to False. This means that the list ["TutorialsPoint", "code"] was not found in 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.

Method 3: Usage of in operator with if statement.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input nested list and give it some random list values.

  • Check if the element is present in the list or not using the in operator and if statement.

  • The if statement executes only if the result returned by the in operator is true i.e if the element is present in the list.

  • Print the result tuple after conversion from a list into a tuple.

  • Print the data type of the result tuple using the type() function for verification. Write the else statement if the element is not present in the 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 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

On executing, the above program will generate the following output −

{TutorialsPoint} Element is in the given list
{bigdata} Element is not present in the given list

We created a list with some random values and then used the in operator to see if the element "TutorialsPoint" was present in the list. It returns True in this case, indicating that the if statement will be executed. The second element, "bigdata," is not in the list, so the operator returns False. As a result, the else statement will be executed.

Conclusion

We learned how to convert a list to a tuple using three different methods: directly utilizing the tuple() function, unpacking the list, and using a for loop within the tuple() function. So, in this tutorial, we learned how the in operator in Python works in a list. We also learned how to use the in operator in conjunction with if statements. We learned how to use the in operator in nested lists or How will the in operator be used to find multiple elements.

Updated on: 03-Nov-2023

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements