Say I have list of tuples:
list = [(1,5), (1,7), (2,3)]
Is there a way in Python to write something like
if (1, *) in list: do things
where *
means "I don’t care about this value"? So we are checking if there is a tuple with 1
at the first position and with whatever value on the second one.
As far as I know there are special mechanisms in other languages, but I just don’t know the name of this particular problem. So is there similar behavior in Python?
P.S.: I know that I can use list comprehensions here. I am just interested in this particular mechanism.
In the majority of programming languages when you need to access a nested data type (such as arrays, lists, or tuples), you append the brackets to get to the innermost item. The first bracket gives you the location of the tuple in your list. The second bracket gives you the location of the item in the tuple.
We can create a list of tuples i.e. the elements of the tuple can be enclosed in a list and thus will follow the characteristics in a similar manner as of a Python list. Since, Python Tuples utilize less amount of space, creating a list of tuples would be more useful in every aspect.
Method 1: By using a loop: We will iterate through each items 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.
Use indexing to get the first element of each tuple Use a for-loop to iterate through a list of tuples. Within the for-loop, use the indexing tuple[0] to access the first element of each tuple, and append it.
You can use the any()
function:
if any(t[0] == 1 for t in yourlist):
This efficiently tests and exits early if 1
is found in the first position of a tuple.
A placeholder object like you're asking for isn't supported natively, but you can make something like that yourself:
class Any(object): def __eq__(self, other): return True ANYTHING = Any() lst = [(1,5), (1,7), (2,3)]
The __eq__
method defines how two objects test for equality. (See https://docs.python.org/3/reference/datamodel.html for details.) Here, ANYTHING
will always test positive for equality with any object. (Unless that object also overrode __eq__
in a way to return False.)
The in
operator merely calls __eq__
for each element in your list. I.e. a in b
does something like:
for elem in b: if elem == a: return True
This means that, if you say (1, ANYTHING) in lst
, Python will first compare (1, ANYTHING)
to the first element in lst
. (Tuples, in turn, define __eq__
to return True if all its elements' __eq__
return True. I.e. (x, y) == (a, b)
is equivalent to x==a and y==b
, or x.__eq__(a) and y.__eq__(b)
.)
Hence, (1, ANYTHING) in lst
will return True, while (3, ANYTHING) in lst
will return False.
Also, note that I renamed your list lst
instead of list
to prevent name clashes with the Python built-in list
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With