Access list elements that are not equal to a specific value

Viewed 7173

I am searching through a list like this:

my_list = [['a','b'],['b','c'],['a','x'],['f','r']]

and I want to see which elements come with 'a'. So first I have to find lists in which 'a' occurs. Then get access to the other element of the list. I do this by abs(pair.index('a')-1)

for pair in my_list:
    if 'a' in pair:
       print( pair[abs(pair.index('a')-1)] )

Is there any better pythonic way to do that?
Something like: pair.index(not 'a') maybe?

UPDATE:

  • Maybe it is good to point out that 'a' is not necessarily the first element.

  • in my case, ['a','a'] doesn't happen, but generally maybe it's good to choose a solution which handles this situation too

3 Answers
Related