Search code examples
pythonpython-2.7nameerror

NameError: global name '_' is not defined


The code:

if (query_id, _) in hashtable[bucket]:

I expected this to work like in a for loop, but instead it gives this error:

NameError: global name '_' is not defined

hastable[bucket] is a list of pairs if that matters (which I doubt). Any ideas?


Solution

  • The x in y is not magic; it's basically the same as y.__contains__(x). Therefore, in cannot search with placeholders; the left argument is fully evaluated. Instead, use

    if any(query_id == qid for (qid, _) in hashtable[bucket]):