nodes: 0, module; 1, function_definition; 2, function_name:nsmallest; 3, parameters; 4, block; 5, identifier:n; 6, identifier:iterable; 7, default_parameter; 8, expression_statement; 9, comment:# Short-cut for n==1 is to use min(); 10, if_statement; 11, comment:# When n>=size, it's faster to use sorted(); 12, try_statement; 13, comment:# When key is none, use simpler decoration; 14, if_statement; 15, comment:# General case, slowest method; 16, expression_statement; 17, expression_statement; 18, if_statement; 19, expression_statement; 20, expression_statement; 21, expression_statement; 22, expression_statement; 23, for_statement; 24, expression_statement; 25, return_statement; 26, identifier:key; 27, None; 28, comment:"""Find the n smallest elements in a dataset.
  
      Equivalent to:  sorted(iterable, key=key)[:n]
      """; 29, comparison_operator:n == 1; 30, block; 31, block; 32, except_clause; 33, else_clause; 34, comparison_operator:key is None; 35, block; 36, assignment; 37, assignment; 38, not_operator; 39, block; 40, call; 41, assignment; 42, assignment; 43, assignment; 44, identifier:elem; 45, identifier:it; 46, block; 47, call; 48, list_comprehension; 49, identifier:n; 50, integer:1; 51, expression_statement; 52, expression_statement; 53, if_statement; 54, return_statement; 55, expression_statement; 56, tuple; 57, block; 58, block; 59, identifier:key; 60, None; 61, expression_statement; 62, comment:# put the range(n) first so that zip() doesn't; 63, comment:# consume one too many elements from the iterator; 64, expression_statement; 65, if_statement; 66, expression_statement; 67, expression_statement; 68, expression_statement; 69, expression_statement; 70, for_statement; 71, expression_statement; 72, return_statement; 73, identifier:it; 74, call; 75, identifier:result; 76, list_comprehension; 77, identifier:result; 78, return_statement; 79, identifier:_heapify_max; 80, argument_list; 81, identifier:top; 82, subscript; 83, identifier:order; 84, identifier:n; 85, identifier:_heapreplace; 86, identifier:_heapreplace_max; 87, expression_statement; 88, if_statement; 89, attribute; 90, argument_list; 91, subscript; 92, for_in_clause; 93, assignment; 94, assignment; 95, comparison_operator:key is None; 96, block; 97, else_clause; 98, conditional_expression:[] if result is sentinel else [result]; 99, assignment; 100, identifier:TypeError; 101, identifier:AttributeError; 102, pass_statement; 103, if_statement; 104, assignment; 105, assignment; 106, not_operator; 107, block; 108, call; 109, assignment; 110, assignment; 111, assignment; 112, identifier:elem; 113, identifier:it; 114, block; 115, call; 116, list_comprehension; 117, identifier:iter; 118, argument_list; 119, tuple; 120, for_in_clause; 121, identifier:result; 122, identifier:result; 123, subscript; 124, integer:0; 125, assignment; 126, comparison_operator:k < top; 127, block; 128, identifier:result; 129, identifier:sort; 130, identifier:r; 131, integer:2; 132, identifier:r; 133, identifier:result; 134, identifier:it; 135, call; 136, identifier:sentinel; 137, call; 138, identifier:key; 139, None; 140, expression_statement; 141, block; 142, list; 143, comparison_operator:result is sentinel; 144, list; 145, identifier:size; 146, call; 147, comparison_operator:n >= size; 148, block; 149, identifier:it; 150, call; 151, identifier:result; 152, list_comprehension; 153, identifier:result; 154, return_statement; 155, identifier:_heapify_max; 156, argument_list; 157, identifier:top; 158, subscript; 159, identifier:order; 160, identifier:n; 161, identifier:_heapreplace; 162, identifier:_heapreplace_max; 163, if_statement; 164, attribute; 165, argument_list; 166, subscript; 167, for_in_clause; 168, identifier:iterable; 169, call; 170, identifier:i; 171, identifier:elem; 172, pattern_list; 173, call; 174, identifier:result; 175, integer:0; 176, identifier:k; 177, call; 178, identifier:k; 179, identifier:top; 180, expression_statement; 181, expression_statement; 182, expression_statement; 183, identifier:iter; 184, argument_list; 185, identifier:object; 186, argument_list; 187, assignment; 188, expression_statement; 189, identifier:result; 190, identifier:sentinel; 191, identifier:result; 192, identifier:len; 193, argument_list; 194, identifier:n; 195, identifier:size; 196, return_statement; 197, identifier:iter; 198, argument_list; 199, tuple; 200, for_in_clause; 201, identifier:result; 202, identifier:result; 203, subscript; 204, integer:0; 205, comparison_operator:elem < top; 206, block; 207, identifier:result; 208, identifier:sort; 209, identifier:r; 210, integer:0; 211, identifier:r; 212, identifier:result; 213, identifier:key; 214, argument_list; 215, identifier:i; 216, identifier:elem; 217, identifier:zip; 218, argument_list; 219, identifier:key; 220, argument_list; 221, call; 222, assignment; 223, augmented_assignment; 224, identifier:iterable; 225, identifier:result; 226, call; 227, assignment; 228, identifier:iterable; 229, subscript; 230, identifier:iterable; 231, identifier:elem; 232, identifier:i; 233, pattern_list; 234, call; 235, identifier:result; 236, integer:0; 237, identifier:elem; 238, identifier:top; 239, expression_statement; 240, expression_statement; 241, expression_statement; 242, identifier:elem; 243, call; 244, identifier:it; 245, identifier:elem; 246, identifier:_heapreplace; 247, argument_list; 248, identifier:top; 249, subscript; 250, identifier:order; 251, integer:1; 252, identifier:min; 253, argument_list; 254, identifier:result; 255, call; 256, call; 257, slice; 258, identifier:i; 259, identifier:elem; 260, identifier:zip; 261, argument_list; 262, call; 263, assignment; 264, augmented_assignment; 265, identifier:range; 266, argument_list; 267, identifier:result; 268, tuple; 269, subscript; 270, integer:0; 271, identifier:it; 272, keyword_argument; 273, identifier:min; 274, argument_list; 275, identifier:sorted; 276, argument_list; 277, identifier:n; 278, call; 279, identifier:it; 280, identifier:_heapreplace; 281, argument_list; 282, identifier:top; 283, subscript; 284, identifier:order; 285, integer:1; 286, identifier:n; 287, identifier:k; 288, identifier:order; 289, identifier:elem; 290, identifier:result; 291, integer:0; 292, identifier:default; 293, identifier:sentinel; 294, identifier:it; 295, keyword_argument; 296, keyword_argument; 297, identifier:iterable; 298, keyword_argument; 299, identifier:range; 300, argument_list; 301, identifier:result; 302, tuple; 303, subscript; 304, integer:0; 305, identifier:default; 306, identifier:sentinel; 307, identifier:key; 308, identifier:key; 309, identifier:key; 310, identifier:key; 311, identifier:n; 312, identifier:elem; 313, identifier:order; 314, identifier:result; 315, integer:0
code: def nsmallest(n, iterable, key=None):
      """Find the n smallest elements in a dataset.
  
      Equivalent to:  sorted(iterable, key=key)[:n]
      """
  
      # Short-cut for n==1 is to use min()
      if n == 1:
          it = iter(iterable)
          sentinel = object()
          if key is None:
              result = min(it, default=sentinel)
          else:
              result = min(it, default=sentinel, key=key)
          return [] if result is sentinel else [result]
  
      # When n>=size, it's faster to use sorted()
      try:
          size = len(iterable)
      except (TypeError, AttributeError):
          pass
      else:
          if n >= size:
              return sorted(iterable, key=key)[:n]
  
      # When key is none, use simpler decoration
      if key is None:
          it = iter(iterable)
          # put the range(n) first so that zip() doesn't
          # consume one too many elements from the iterator
          result = [(elem, i) for i, elem in zip(range(n), it)]
          if not result:
              return result
          _heapify_max(result)
          top = result[0][0]
          order = n
          _heapreplace = _heapreplace_max
          for elem in it:
              if elem < top:
                  _heapreplace(result, (elem, order))
                  top = result[0][0]
                  order += 1
          result.sort()
          return [r[0] for r in result]
  
      # General case, slowest method
      it = iter(iterable)
      result = [(key(elem), i, elem) for i, elem in zip(range(n), it)]
      if not result:
          return result
      _heapify_max(result)
      top = result[0][0]
      order = n
      _heapreplace = _heapreplace_max
      for elem in it:
          k = key(elem)
          if k < top:
              _heapreplace(result, (k, order, elem))
              top = result[0][0]
              order += 1
      result.sort()
      return [r[2] for r in result]