def find_common_parent(a, b):
 a_parents = list(a.iterancestors())
 b_parents = list(b.iterancestors())
 a_parents_set = set(a_parents)
 b_parents_set = set(b_parents)
 if (a == b):
  return a
 if (b in a_parents_set):
  return b
 if (a in b_parents_set):
  return a
 if (len(a_parents) < len(b_parents)):
  for elem in a_parents:
   if ((elem == b) or (elem in b_parents_set)):
    return elem
 else:
  for elem in b_parents:
   if ((elem == a) or (elem in a_parents_set)):
    return elem

def find_common_parent ( a , b ) : a_parents = list ( a . iterancestors ( ) ) b_parents = list ( b . iterancestors ( ) ) a_parents_set = set ( a_parents ) b_parents_set = set ( b_parents ) if ( a == b ) : return a if ( b in a_parents_set ) : return b if ( a in b_parents_set ) : return a if ( len ( a_parents ) < len ( b_parents ) ) : for elem in a_parents : if ( ( elem == b ) or ( elem in b_parents_set ) ) : return elem else : for elem in b_parents : if ( ( elem == a ) or ( elem in a_parents_set ) ) : return elem
find a common parent for 2 elements .