So, it's possible that the above answers the question I have and I'm just not clever enough to see the answer. But: I had a case where I wanted to extract and return the top N elements of a list (where "top" meant "smallest numeric value"; the alternative for "largest numeric value" is a trivial modification). To me, the obvious answer was: def topNElements(lst, n): return lst.sort()[:n] But, of course, that fails with a TypeError, because lst.sort() returns None, which is not subscriptable. The function really needs to be: def topNElements(lst, n): lst.sort() return lst[:n] It gets worse if I want to include an invariant in the list before I sort it: def bogusTopNElementsWithInvariant(lst, n): return lst.append(INVARIANT).sort()[:n] def topNElementsWithInvariant(lst, n) lst.append(INVARIANT) lst.sort() return lst[:n] So, my question is: Someone obviously thought that it was wise and proper to require the longer versions that I write above. Why? B. -- Brendon Towle, PhD Cognitive Scientist +1-412-690-2442x127 Carnegie Learning, Inc. The Cognitive Tutor Company ® Helping over 375,000 students in 1000 school districts succeed in math. |
-- http://mail.python.org/mailman/listinfo/python-list