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
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.
-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.
+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 thelen()function. That only worked because the input list was already sorted by length. +
Are you watching closely?
>>> list(range(0, 3))