Monday, November 2, 2009

Chapter 4 --Page 140-141 -- Listing 4.7

The values method returns a dict_values object, not a list. However, it can be turned into a list by using the list() function. Line 10 of listing 4.7 should be

countlist = list(countlist.values())

Then the max function is legal on countlist (as shown on line 11).

The text on page 140 should read

Recall that the values() method returns a dict_values object which can be turned into a list by applying the list function.

2 comments:

  1. Actually, the max() function can be applied to any iterable, including a dict_values object as well as a list. So although the name countlist would be a misnomer without the use of list(), and although the text on page 140 wasn't right, the code actually does work as given.

    ReplyDelete
  2. I think the line of code should read:

    countlist = list(countdict.values())

    since "countlist" has not been defined until this lines executes.

    ReplyDelete