I would use a different function name, different argument names and a docstring to make it clear what's going on, something like:
def get_other(current, both):
"""Return the element from 'both' that is not 'current'."""
...
Note that both
implies a pair without anything long-winded, and doesn't specify the type
required.
You can use your own implementation or Joel's; as long as the function does what it says, it doesn't really matter how it's implemented (barring performance concerns, edge cases, etc.).
However, to work with non-indexable containers (like the set
, or the keys of a dict
) without having to explicitly test what both
is, I would probably go for:
def get_other(current, both):
"""Return the element from 'both' that is not 'current'."""
for item in both:
if item != current:
return item
Indeed, if len(both) > 2
, this will just return the first item in it that isn't equal to current
- you can add checks for this if this is not the desired behaviour.