diff --git a/advanced-iterators.html b/advanced-iterators.html index 1f9751c..5487223 100755 --- a/advanced-iterators.html +++ b/advanced-iterators.html @@ -365,9 +365,14 @@ Lizzie Wesley
  1. The itertools.groupby() function takes a sequence and a key function, and returns an iterator that generates pairs. Each pair contains the result of key_function(each item) and another iterator containing all the items that shared that key result. -
  2. In this example, given a list of names sorted by length, itertools.groupby(names, len) will put all the 4-letter names in one iterator, all the 5-letter names in another iterator, and so on. The groupby() function is completely generic; it could group strings by first letter, numbers by their number of factors, or any other key function you can think of. +
  3. In this example, given a list of names already sorted by length, itertools.groupby(names, len) will put all the 4-letter names in one iterator, all the 5-letter names in another iterator, and so on. The groupby() function is completely generic; it could group strings by first letter, numbers by their number of factors, or any other key function you can think of.
+ +
+

The itertools.groupby() function only works if the input sequence is already sorted by the grouping function. In the example above, you grouped a list of names by the len() function. That only worked because the input list was already sorted by length. +

+

Are you watching closely?

 >>> list(range(0, 3))