Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find tuple structure containing an unknown value inside a list

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.

like image 717
selotec Avatar asked Mar 01 '16 09:03

selotec


People also ask

How do you find the elements in a list of tuples?

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.

Can you have a tuple inside a list?

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.

How do you check if a tuple contains a value?

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.

How do you get one part of a tuple?

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.


2 Answers

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.

like image 77
Martijn Pieters Avatar answered Oct 08 '22 12:10

Martijn Pieters


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.

like image 23
acdr Avatar answered Oct 08 '22 12:10

acdr