Apologies for the churn. I looked at the underlying code for the API methods I used and found a significant inefficiency in what I proposed. The underlying reversed dictionary {id:name} was being generated for *every* field item. For big libraries this could be a significant performance hit.
Here is a version that reverses the id map once then uses it to build the output.
Code:
def ids_for_field(db, ids_of_books, field_name):
# First get all the names for the desired books.
# Use a set to make them unique
unique_names = set()
for tup in db.all_field_for(field_name, ids_of_books).values():
for val in tup:
unique_names.add(val)
# reverse the map of ids to names so id_map[name] gives the id
id_map = {v:k for k,v in db.get_id_map(field_name).items()}
# Now build the pairs (id, name)
id_field_pairs = list()
for name in unique_names:
id_field_pairs.append((id_map[name], name))
return id_field_pairs