33

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.

10
  • 22
    Side note: don't name your variable list. This will clash with the list function and data type from the Python standard library. Mar 1, 2016 at 10:04
  • Related: How Python's any and all functions work? Mar 1, 2016 at 11:56
  • 3
    You have selected the wrong answer. -1 Mar 1, 2016 at 21:59
  • 1
    @Justin: sure, but then wrap that SELECT in a WHERE EXISTS clause.
    – Martijn Pieters
    Mar 2, 2016 at 13:53
  • 1
    The name of the language feature is QbE (query by example), unification (Prolog), or simply "pattern matching".
    – jhermann
    Mar 4, 2016 at 18:43

7 Answers 7

71

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.

9
  • 3
    For what it's worth, I down-voted this answer, despite the fact that this is how I would solve OP's posted problem. However, considering the title of the thread, I don't think this answer (or any other current ones apart from my own) actually say what the OP wants to know (namely how to get a variable with "any" value).
    – acdr
    Mar 1, 2016 at 10:44
  • 13
    @acdr: that's a different interpretation of the question. I'm sorry you don't find my answer helpful or enlightening, but this is a correct way to solve the underlying problem.
    – Martijn Pieters
    Mar 1, 2016 at 10:46
  • 1
    It's a good solution, thanks, but a question was a slightly different.
    – selotec
    Mar 1, 2016 at 10:50
  • 1
    The difference between this and OP is that this approach will iterate over yourlist. This is okay when yourlist is actually a list but when it is for example a 2d-tree, things will be more expensive. Mar 1, 2016 at 14:14
  • 3
    @WorldSEnder: alternative data structures are entirely out of scope here; the OP did post a list in their question.
    – Martijn Pieters
    Mar 1, 2016 at 14:15
33

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.

15
  • 10
    Pre-empting other comments: technically when doing trickery that involves redefining __eq__, one should also redefine __ne__, but that's not needed in this case. Also note that ANYTHING==x will return True regardless of x's value, but the same is not necessarily true of x==ANYTHING.
    – acdr
    Mar 1, 2016 at 10:23
  • 1
    There are other answers which talk about built in 'any' , Why do you think this isn't supported natively?
    – AlokThakur
    Mar 1, 2016 at 10:27
  • 2
    @AlokThakur: There's two different functionalities here that sound very similar. It is indeed natively supported to check if any element of an iterable is True, but the OP didn't sound like that's what he wanted to know. The second issue, which is far more interesting, is having an object (* in OP's post, ANYTHING in mine) which is a placeholder for "any value", which is unrelated to the functionality provided by the any built-in. (That said, I edited my answer to clarify what isn't natively supported.)
    – acdr
    Mar 1, 2016 at 10:31
  • 10
    Be VERY CAREFUL using solutions like this... the given solution will work for a list, but will NOT work for something like a dictionary: d = { (1,1): 'A' }; (1, ANYTHING) in d return False. This is because in this case, it's actually computing the hash of the source and looking up directly. Basically, anytime the object's lookup is O(1), it won't work without explicitly converting it to an O(n) lookup (e.g. (1, ANYTHING) in d.keys() does return True...). Just make sure you don't blindly use this... Mar 1, 2016 at 16:53
  • 6
    If I saw code like this in a review I would kill it with fire and insist it be done as Martijn suggested. Mar 1, 2016 at 21:57
11

Not all of my solution methods provided below will be necessarily efficient. My goal is to demonstrate every possible solution method I can think of - at the end of my answer I provide "benchmark" results to show why or why not you should use one certain method over another. I believe that is a good way of learning, and I will shamelessly encourage such learning in my answers.


Subset + hash sets

>>> a_list = [(1,5), (1,7), (2,3)]
>>>
>>> set([l[0] for l in a_list])
{1, 2}
>>>
>>> 1 in set([l[0] for l in a_list])
True

map(), and anonymous functions

>>> a_list = [(1,5), (1,7), (2,3)]
>>>
>>> map(lambda x: x[0] == 1, a_list)
[True, True, False]
>>>
>>> True in set(map(lambda x: x[0] == 1, a_list))
True

filter and anonymous functions

>>> a_list = [(1,5), (1,7), (2,3)]
>>>
>>> filter(lambda x: x[0] == 1, a_list)
[(1,5), (1,7)]
>>>
>>> len(filter(lambda x: x[0] == 1, a_list)) > 0  # non-empty list
True

MICROBENCHMARKS

Conditions

  • 1000 items
  • 100K repetition
  • 0-100 random range
  • Python 2.7.10, IPython 2.3.0

Script

from pprint import pprint
from random import randint
from timeit import timeit

N_ITEMS = 1000
N_SIM = 1 * (10 ** 5)  # 100K = 100000

a_list = [(randint(0, 100), randint(0, 100)) for _ in range(N_ITEMS)]

set_membership_list_comprehension_time = timeit(
    "1 in set([l[0] for l in a_list])",
    number = N_SIM,
    setup="from __main__ import a_list"
)

bool_membership_map_time = timeit(
    "True in set(map(lambda x: x[0] == 1, a_list))",
    number = N_SIM,
    setup="from __main__ import a_list"
)

nonzero_length_filter_time = timeit(
    "len(filter(lambda x: x[0] == 1, a_list)) > 0",
    number = N_SIM,
    setup="from __main__ import a_list"
)

any_list_comprehension_time = timeit(
    "any(t[0] == 1 for t in a_list)",
    number = N_SIM,
    setup="from __main__ import a_list"
)

results = {
    "any(t[0] == 1 for t in a_list)": any_list_comprehension_time,
    "len(filter(lambda x: x[0] == 1, a_list)) > 0": nonzero_length_filter_time,
    "True in set(map(lambda x: x[0] == 1, a_list))": bool_membership_map_time,
    "1 in set([l[0] for l in a_list])": set_membership_list_comprehension_time
}

pprint(
    sorted(results.items(), key = lambda x: x[1])
)

Results (in seconds)

[('any(t[0] == 1 for t in a_list)', 2.6685791015625),     # winner - Martijn
 ('1 in set([l[0] for l in a_list])', 4.85234808921814),
 ('len(filter(lambda x: x[0] == 1, a_list)) > 0', 7.11224889755249),
 ('True in set(map(lambda x: x[0] == 1, a_list))', 10.343087911605835)]

Who's got the last laugh now? ... Martijn (at least I tried)

MORAL OF THE STORY: Don't spend more than 10 minutes "proving" your inferior solution is faster and more efficient on a small test data, when another user's answer is the de-facto correct one

13
  • The map() defeats the purpose of any(); you now created a list with all boolean outcomes, up front. That's why you normally use a generator expression with any().
    – Martijn Pieters
    Mar 1, 2016 at 10:16
  • You created a set from a list; you can create the set directly, again with a generator expression rather than a list comprehension.
    – Martijn Pieters
    Mar 1, 2016 at 10:17
  • 1
    Perhaps ask someone else to read [..] it taught me not to be cocky and that we can all learn a thing or two from each other. Except maybe you. Maybe you're already a know it all. I don't know. Thanks anyways and ask them how that comes over? That comment does not come over as appreciative. :-)
    – Martijn Pieters
    Mar 1, 2016 at 10:57
  • 5
    @MartijnPieters: HAHAHAHA I'm sorry - I was actually trying to make it sound like I respect your expertise and knowledge, like you already know everything and that you're the teacher more often than you are the student. But in hindsight man I DO sound like an incredible asshole (pardon my language)! But no, that was my bad, it was not my intention to voice it that way. I've seen you helping others elsewhere and I appreciate what you're doing for the community. Man, I feel sort of bad right now. Apologies my good man! Mar 1, 2016 at 11:00
  • 4
    Kudos for this reaction. We all need to relearn this from time to time.
    – holdenweb
    Mar 1, 2016 at 11:36
5

This can be done in Python using list comprehension. ex:

a= [(1, 2), (3, 4), (4, 5), (1, 4)]
[i for i in a if i[0] == 1]

Will give you:

[(1, 2), (1, 4)]
3

Indexing is the simplest but if you wanted to use syntax similar to your example where you wanted to assign the first value to a variable and ignore the rest you could use python3's extended iterable unpacking.

In [3]: [a for a,*_ in l]
Out[3]: [1, 1, 2]

Or with the any logic:

In [4]: l = [(1,5), (1,7), (2,3)]

In [5]: any(a == 1 for a,*_ in l)
Out[5]: True

Or mimicking any without the function call:

In [23]: l = [(1,5), (1,7), (2,3)]
In [24]: g = (a  for a,*_ in l)

In [25]: 1 in g
Out[25]: True

In [26]: list(g)
Out[26]: [1, 2]
2

number of element in tuple could be handled also.

>>> import operator
>>> mylist = [(1,2), (1,5), (4,5,8)]
>>> any(i==1 for i  in map(operator.itemgetter(0), mylist))
True
0

It sounds like you actually want filter(), not any():

tuple_list = [(1,5), (1,7), (2,3)]
for pair in filter(lambda pair: (pair[0] == 1), tuple_list):
    print "Second value {pair[1]} found from {pair}".format(pair=pair)
...
Second value 5 found from (1, 5)
Second value 7 found from (1, 7)

The filter() method is great because you can provide a function directly to it. This lets you specify a certain key to filter on, etc. To simplify it further, use a lambda expression to make the entire thing into a one-liner.

1
  • The purpose of my answer is to show the Python 2 filter() method specifically as a correction to the any() method; not to necessarily show the cleanest way. Also, according to post OP is already aware of list comprehensions.
    – dhackner
    Mar 1, 2016 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.