nodes: 0, module; 1, function_definition; 2, function_name:fmt_pairs; 3, parameters; 4, block; 5, identifier:obj; 6, default_parameter; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, if_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, return_statement; 16, identifier:indent; 17, integer:4; 18, identifier:sort_key; 19, None; 20, comment:"""Format and sort a list of pairs, usually for printing.
  
      If sort_key is provided, the value will be passed as the
      'key' keyword argument of the sorted() function when
      sorting the items. This allows for the input such as
      [('A', 3), ('B', 5), ('Z', 1)] to be sorted by the ints
      but formatted like so:
  
          l = [('A', 3), ('B', 5), ('Z', 1)]
          print(fmt_pairs(l, sort_key=lambda x: x[1]))
  
              Z 1
              A 3
              B 5
          where the default behavior would be:
  
          print(fmt_pairs(l))
  
              A 3
              B 5
              Z 1
      """; 21, assignment; 22, not_operator; 23, block; 24, assignment; 25, assignment; 26, assignment; 27, assignment; 28, identifier:string; 29, identifier:lengths; 30, list_comprehension; 31, identifier:lengths; 32, return_statement; 33, identifier:longest; 34, call; 35, identifier:obj; 36, call; 37, identifier:formatter; 38, binary_operator:'%s{: <%d} {}' % (' ' * indent, longest); 39, identifier:string; 40, call; 41, call; 42, for_in_clause; 43, string; 44, identifier:max; 45, argument_list; 46, identifier:sorted; 47, argument_list; 48, string; 49, tuple; 50, attribute; 51, argument_list; 52, identifier:len; 53, argument_list; 54, identifier:x; 55, identifier:obj; 56, identifier:lengths; 57, identifier:obj; 58, keyword_argument; 59, string_content:%s{: <%d} {}; 60, binary_operator:' ' * indent; 61, identifier:longest; 62, string; 63, identifier:join; 64, list_comprehension; 65, subscript; 66, identifier:key; 67, identifier:sort_key; 68, string; 69, identifier:indent; 70, string_content; 71, call; 72, for_in_clause; 73, identifier:x; 74, integer:0; 75, string_content:; 76, escape_sequence:\n; 77, attribute; 78, argument_list; 79, pattern_list; 80, identifier:obj; 81, identifier:formatter; 82, identifier:format; 83, identifier:k; 84, identifier:v; 85, identifier:k; 86, identifier:v
code: def fmt_pairs(obj, indent=4, sort_key=None):
      """Format and sort a list of pairs, usually for printing.
  
      If sort_key is provided, the value will be passed as the
      'key' keyword argument of the sorted() function when
      sorting the items. This allows for the input such as
      [('A', 3), ('B', 5), ('Z', 1)] to be sorted by the ints
      but formatted like so:
  
          l = [('A', 3), ('B', 5), ('Z', 1)]
          print(fmt_pairs(l, sort_key=lambda x: x[1]))
  
              Z 1
              A 3
              B 5
          where the default behavior would be:
  
          print(fmt_pairs(l))
  
              A 3
              B 5
              Z 1
      """
      lengths = [len(x[0]) for x in obj]
      if not lengths:
          return ''
      longest = max(lengths)
      obj = sorted(obj, key=sort_key)
      formatter = '%s{: <%d} {}' % (' ' * indent, longest)
      string = '\n'.join([formatter.format(k, v) for k, v in obj])
      return string