How to check if a tuple contains an element in Python

How to check if a tuple contains an element in Python:

In this post, we will learn how to check if a Python tuple contains an element. There are different ways to check that. We can either iterate through its items or we can use an if-else block to do that.

In this post, I will show you how to check if a tuple contains an item in three different ways.

Method 1: By using a loop:

Let’s implement it with a loop. We will iterate through each item of the tuple one by one and compare it with the given value. If any value in the tuple is equal to the given value, it will return True. Else, it will return False.

Below is the complete program:

def contains(tuple, given_char):
    for ch in tuple:
        if ch == given_char:
            return True
    return False


given_tuple = ("a", "b", "c", "d", "e", "f", "g", "h")

print(f"Given tuple: {given_tuple}")
char = input("Enter a character to find: ")

if contains(given_tuple, char):
    print("It is in the tuple")
else:
    print("It is not in the tuple")

Download it on GitHub

Here,

  • The contains method is used to check if a character is in the tuple given_tuple or not.
    • This method returns one boolean value. Based on the return value of this method, it prints a message to the user.
  • The contains method takes one tuple and one character as the parameters. It iterates through the items of the tuple and if it finds any item equal to the provided character, it returns True. Else, it returns False if the character is not found in the tuple.
  • It takes one character as input from the user and calls the contains method to check if that character is in the tuple or not.

If you run this program, it will print outputs as below:

Given tuple: ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
Enter a character to find: i
It is not in the tuple

Given tuple: ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
Enter a character to find: e
It is in the tuple

We can also use this approach with a tuple of objects. e.g.:

def contains(tuple, item):
    for i in tuple:
        if i == item:
            return True
    return False


given_tuple = ({"id": 1, "name": "Alex"}, {"id": 2, "name": "Bob"})

print(f"Given tuple: {given_tuple}")
item = {"id": 4, "name": "Bob"}

if contains(given_tuple, item):
    print(f"{item} is in the tuple")
else:
    print(f"{item} is not in the tuple")

Download it on GitHub

It will print:

Given tuple: ({'id': 1, 'name': 'Alex'}, {'id': 2, 'name': 'Bob'})
{'id': 4, 'name': 'Bob'} is not in the tuple

Instead of comparing with the == operator, you can also add any other comparison function to compare two items.

Method 2: By using if..not:

We can also quickly check if an element is in a tuple or not by using one if..not check. Let me change the above program to use if..not:

def contains(tuple, given_char):
    if given_char in tuple:
        return True
    return False


given_tuple = ("a", "b", "c", "d", "e", "f", "g", "h")

print(f"Given tuple: {given_tuple}")
char = input("Enter a character to find: ")

if contains(given_tuple, char):
    print("It is in the tuple")
else:
    print("It is not in the tuple")

Download it on GitHub

It will print similar output:

Given tuple: ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
Enter a character to find: g
It is in the tuple

Python check if a tuple contains an element

As you can see here, we can do it easily with only one line. No need to iterate over the tuple.

You might also like: