How to check if a tuple contains an element in Python?
Asked Answered
P

5

34

I tried to find the available methods but couldn't find it. There is no contains. Should I use index? I just want to know if the item exists, don't need the index of it.

Perionychium answered 29/7, 2013 at 9:16 Comment(0)
G
88

You use in.

if element in thetuple:
    #whatever you want to do.
Gaillard answered 29/7, 2013 at 9:18 Comment(8)
Thanks, for multiple elements should I do if a in tuple and b in tuple: ?Perionychium
Yes. If you have a lot of elements you might consider using sets instead, where you can do union, difference and intersection operations.Gaillard
Thanks, actually the API I am using is returning a tuple, that's why I was using that. Should I convert it to a set?Perionychium
@JoanVenge: If you want it to be a set, yes.Gaillard
Thanks will do. I just didn't want to change the data types too much because it's gonna get called 10000s of times every frame.Perionychium
@JoanVenge: Yes, making a set of a tuple is a heavy operation, so doing that 10.0000 times is generally not desirable. You have a specific situation and a specific problem, but you ask very generic questions. The answers you get will not be very useful.Gaillard
Ok thanks, I will just use in twice because it's only 2 checks. I just didn't want to make it very messy.Perionychium
Then using a set is not useful.Gaillard
M
11
if "word" in str(tuple):
# You can convert the tuple to str too

i has the same problem and only worked to me after the convert str()

Memoir answered 1/9, 2019 at 23:3 Comment(4)
Hi new contributor. Your answer was similar as accepted answer, but you should know that keyword in is same as __contains__ overloaded operator on class. So when original poster said that there was no __contains__ he/she meant that it wasn't possible to use in keyword on data set. As you can see string class has that operator implemented so your answer is ok. Keep going! :DEnumeration
its true, i was not seen the specification in the question. I was looking to solve my problem (a 'str' situation) and I found that the question wasnt about it only after the answer hahaMemoir
That's not good solution, since it will return true even if the tuple contains "sword".Marvel
Good solution indeed. Lennart Regebro's solution doesn't works for me.Kunin
V
1

Be careful with that: return Oops. use Set: d= {...}

def simha():
    d = ('this_is_valid')
    b = 'valid'
    if b in d:
        print("Oops!!!!!")


simha()
Vickers answered 7/1, 2018 at 15:55 Comment(1)
tuples are only created in python if you use the notation (element,). The comma is very important. If there is no comma, python treats it like an expression to evaluate.Guru
D
1

Use as follows: variable_name in tuple

animals = ('Dog', 'Elephant', 'Fox')

animal_name = 'Fox'

Check if an x is in animals tuple:

if animal_name in animals :
    # Fox is existing in animals

Check if an x is NOT in animals tuple:

if not animal_name in animals :
    # Fox is not existing in animals
Demetriusdemeyer answered 8/1, 2023 at 12:14 Comment(0)
S
0

Try this:

fruits = ['peach', 'blueberry', 'raspberry', 'apple', 'orange',  'lemon', 'banana']
first, second, *others = fruits

print(others) # ['raspberry', 'apple', 'orange', 'lemon', 'banana']
Shelter answered 17/9, 2023 at 7:2 Comment(1)
This answer doesn't make sense with the question...Pious

© 2022 - 2024 — McMap. All rights reserved.