code: def is_mass_balanced(reaction):
      
      balance = defaultdict(int)
      for metabolite, coefficient in iteritems(reaction.metabolites):
          if metabolite.elements is None or len(metabolite.elements) == 0:
              return False
          for element, amount in iteritems(metabolite.elements):
              balance[element] += coefficient * amount
      return all(amount == 0 for amount in itervalues(balance))
docs: Confirm that a reaction is mass balanced.