From 06a1c5f95ae4ce4f007d21b69f2a095d7cd4412b Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Sun, 5 Jul 2009 06:53:19 -0400 Subject: [PATCH] mention lstrip() and strip() --- advanced-iterators.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-iterators.html b/advanced-iterators.html index f299960..fce7dc8 100644 --- a/advanced-iterators.html +++ b/advanced-iterators.html @@ -295,7 +295,7 @@ StopIteration 'Chris', 'Ethan', 'Sarah', 'Lizzie', 'Wesley']
  1. This idiom returns a list of the lines in a text file. -
  2. Unfortunately (for this example), the list(open(filename)) idiom also includes the carriage returns at the end of each line. This list comprehension uses the rstrip() string method to strip trailing whitespace from each line. +
  3. Unfortunately (for this example), the list(open(filename)) idiom also includes the carriage returns at the end of each line. This list comprehension uses the rstrip() string method to strip trailing whitespace from each line. (Strings also have an lstrip() method to strip leading whitespace, and a strip() method which strips both.)
  4. The sorted() function takes a list and returns it sorted. By default, it sorts alphabetically.
  5. But the sorted() function can also take a function as the key parameter, and it sorts by that key. In this case, the sort function is len(), so it sorts by len(each item). Shorter names come first, then longer, then longest.