Search elements in an array of arrays
Question: Sorry if I get terminology wrong - I've only just started learning Python, and
I'm receiving instruction from friends instead of being on an actual course.

I want to search a list containing lots of arrays containing multiple
elements, and find arrays with some elements matching, but some different.

In less confusing terms e.g. I have a list of arrays that each contain 2
elements (I think this is called a 2D array?) so:

    
    
    list = [[1, 2], [2, 2], [3, 5], [4, 1], [5, 2], ...]
    

In my specific example, the first elements in each sub array just ascend
linearly, but the second elements are almost random. I want to find or sort
the arrays only by the second number. I could just remove the first number
from each array:

    
    
    list = [2, 2, 5, 1, 2 ...]
    

And then use something like "if list[x] == 1" to find '1' etc.  
(side note: I'm not sure how to find ALL the values if one value is repeated -
I can't remember quite what I wrote but it would only ever find the first
instance where the value matched, so e.g. it would detect the first '2' but
not the second or third)

But I want to keep the first values in each array. My friend told me that you
could use a dictionary with values and keys, which would work for my example,
but I want to know what the more general method would be.

So in my example, I hoped that if I wrote this:

    
    
    if list[[?, x]] == [?, 1]
    

Then it would find the array where the second value of the array was 1, (i.e.
[4, 1] in my example) and not care about the first value. Obviously it didn't
work because '?' isn't Python syntax as far as I'm aware, but hopefully you
can see what I'm trying to do?

So for a more general case, if I had a list of 5 dimensional arrays and I
wanted to find the second and fourth values of each array, I would write:

    
    
    if list[[?, x, ?, y, ?]] == [?, a, ?, b, ?]
    

And it would match any array where the value of the second element was 'a',
and the value of the fourth was 'b'.

e.g. [3, a, 4, b, 7], [20, a, 1, b, 9], ['cat', a, 'dog', b, 'fish'] etc.
would all be possible results found by the command.

So I want to know if there's any similar way to my method of using a question
mark (but that actually works) to denote that an element in an array can have
any value.


Answer: To sort on the second element for a list containg lists (or tuples):

    
    
    from operator import itemgetter
    mylist = [[1, 2], [2, 2], [3, 5], [4, 1], [5, 2]]
    sortedlist = sorted(mylist, key=itemgetter(1))
    

See the [Python sorting howto](https://docs.python.org/2/howto/sorting.html).

