Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using collection functions


Python offers a number of functions which work with any kind of collection. These include sorted(), max(), min(), and sum(). We also have some higher-order functions, map(), filter(), and the entire itertools module. We'll address additional higher-order functions in Chapter 8, More Advanced Functions.

The sorted() function returns a sorted list from a collection. It transforms the given collection into a list collection as part of the sorting process. If the collection doesn't define the proper iterator methods, it can't be easily sorted by using this function.

The max() and min() functions reduce a collection to a single value: either the largest or the smallest value in the collection. This reduction presumes that the items can be meaningfully compared. Consider a tuple that has mixed values in it:

((255, 73, 108), 'Radical Red')

We can't meaningfully evaluate max() or min() on a collection of mixed values like this. The functions will be forced to compare a tuple...