code: def sort_dict(key_counts, by_key=False):
      '''Accept a dict of key:values (numerics) returning list of key-value tuples ordered by desc values.
  
      If by_key=True, sorts by dict key.'''
      sort_key = lambda x: (-1 * x[1], x[0])
      if by_key:
          sort_key = lambda x: x[0]
      return sorted(key_counts.items(), key=sort_keyf sort_dict(key_counts, by_key=False):
      '''Accept a dict of key:values (numerics) returning list of key-value tuples ordered by desc values.
  
      If by_key=True, sorts by dict key.'''
      sort_key = lambda x: (-1 * x[1], x[0])
      if by_key:
          sort_key = lambda x: x[0]
      return sorted(key_counts.items(), key=sort_key)
docs: Accept a dict of key:values (numerics) returning list of key-value tuples ordered by desc values.
  
      If by_key=True, sorts by dict key.