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.
How to check if a tuple contains an element in Python?
Asked Answered
You use in
.
if element in thetuple:
#whatever you want to do.
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
set
s 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
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()
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! :D –
Enumeration 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 haha –
Memoir
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
Be careful with that: return Oops. use Set: d= {...}
def simha():
d = ('this_is_valid')
b = 'valid'
if b in d:
print("Oops!!!!!")
simha()
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 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
Try this:
fruits = ['peach', 'blueberry', 'raspberry', 'apple', 'orange', 'lemon', 'banana']
first, second, *others = fruits
print(others) # ['raspberry', 'apple', 'orange', 'lemon', 'banana']
This answer doesn't make sense with the question... –
Pious
© 2022 - 2024 — McMap. All rights reserved.