Ignoring a value in a list search
<p>I am using lists to store attributes and this problem cropped up when I was searching for equal items:</p>

<pre><code>l = [['a',1,2],['b',1,2],['c',2,3],['d',1,2]]
if [any,1,2] in l:
    print ('blue')
elif [any,2,3] in l:
    print('red')
</code></pre>

<p>desired output</p>

<pre><code>blue
</code></pre>

<p>What I want is the first value being ignored/can be any value.
I know the any function doesn't work this way but what is the correct method to doing so?</p>

<p>Perhaps this is more understandable</p>

<pre><code>l = [['a',1,2],['b',1,2],['c',2,3],['d',1,2]]
for a in range(0,4):
    if [1,2] == x[1:] for x in l:
        print ('blue')
    elif [2,3] == x[1:] for x in l:
        print('red')
</code></pre>

<p>for completely generic case simply introduce some magical symbol to denote 'any' like '*' and write your own simple comparator</p>

<pre><code>def matches(element, mask):
  for el, mask_el in zip(element, mask):
    if mask_el != '*' and el != mask_el:
      return False
  return True
</code></pre>

<p>and now you can do</p>

<pre><code>for list_el in list:
  if matches(list_el, ['*', 1, 2]):
    print 'blah'
</code></pre>

<p>the nice thing is that it is very generic, and will work with arbitrary number of '*' in your mask.</p>

python list