B5.write a python program to check whether an element exists within a tuple

Last update on August 19 2022 21:50:46 (UTC/GMT +8 hours)

Python tuple: Exercise-10 with Solution

Write a Python program to check whether an element exists within a tuple.

Sample Solution:-

Python Code:

tuplex = ("w", 3, "r", "e", "s", "o", "u", "r", "c", "e")
print("r" in tuplex)
print(5 in tuplex)

Sample Output:

True                                                                                                          
False 

Pictorial Presentation:

B5.write a python program to check whether an element exists within a tuple

Flowchart:

B5.write a python program to check whether an element exists within a tuple

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the repeated items of a tuple.
Next: Write a Python program to convert a list to a tuple.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sometimes, while working with data, we can have a problem in which we need to check if the data we are working with has a particular element. Let’s discuss certain ways in which this task can be performed. 

    Method #1: Using loop 

    This is a brute force method to perform this task. In this, we iterate through the tuple and check each element if it’s our, if found we return True. 

    Python3

    test_tup = (10, 4, 5, 6, 8)

    print("The original tuple : " + str(test_tup))

    N = 6

    res = False

    for ele in test_tup :

        if N == ele :

            res = True

            break

    print("Does tuple contain required value ? : " + str(res))

    Output : 

    The original tuple : (10, 4, 5, 6, 8)
    Does tuple contain required value ? : True

    Method #2: Using in operator 

    It is used to perform this task. It is a one-liner and recommended to perform this task. 

    Python3

    test_tup = (10, 4, 5, 6, 8)

    print("The original tuple : " + str(test_tup))

    N = 6

    res = N in test_tup

    print("Does tuple contain required value ? : " + str(res))

    Output : 

    The original tuple : (10, 4, 5, 6, 8)
    Does tuple contain required value ? : True

    Method: Using list comprehension method 

    Python3

    t = (10, 4, 5, 6, 8); n=6

    x=[i for i in t if i==n]

    print(["yes" if x else "no"])

    Method: Using lambda function 

    Python3

    t = (10, 4, 5, 6, 8); n=6

    x=tuple(filter(lambda i: (i==n),t))

    print(["yes" if x else "no"])

    Output

    ["yes"]

    Method: Using the enumerate function

    Python3

    t = ('10', '4', '5', '6', '8'); n=6

    x=[int(i) for i in t if int(i)==n]

    print(["yes" if x else "no"])


    How do you check whether an element exists within a tuple in Python?

    Python Exercise: Check whether an element exists within a tuple.
    Sample Solution:-.
    Python Code: tuplex = ("w", 3, "r", "e", "s", "o", "u", "r", "c", "e") print("r" in tuplex) print(5 in tuplex) ... .
    Pictorial Presentation:.
    Flowchart: ... .
    Python Code Editor: ... .
    Have another way to solve this solution?.

    Which operator in Python is used to check the existence of tuple item in a tuple?

    1. Tuple Membership Test. We can test if an item exists in a tuple or not, using the keyword in .

    Which operator is used to determine whether an element is present in a tuple or not?

    in operator can be used to check for the membership of any element in a sequence.

    Which method converts list into the tuple?

    Using the tuple() built-in function An iterable can be passed as an input to the tuple () function, which will convert it to a tuple object. If you want to convert a Python list to a tuple, you can use the tuple() function to pass the full list as an argument, and it will return the tuple data type as an output.