OReilly Learning Python 2nd Edition Dec 2003 ISBN 0596002815

6.4 Dictionaries in Action

  Assuggests, dictionaries are indexed by key, and nested dictionary entries are referenced by a series of indexes (keys in square brackets). When Python creates a dictionary, it stores its items in any order it chooses; to fetch a value back, supply the key that it is associated with. Let's go back to the interpreter to get a feel for some of the dictionary operations in

   .

6.4.1 Basic Dictionary Operations

  The built-in len function works on dictionaries too; it returns the number of items stored away in the dictionary, or equivalently, the length of its keys list. The dictionary has_key method allows you to test for key existence, and the keys method returns all the keys in the dictionary, collected in a list. The latter of these can be useful for processing dictionaries sequentially, but you shouldn't depend on the order of the keys list. Because the keys result is a normal list, however, it can always be sorted if order matters:

  >>> len(d2) # Number of entries in dictionary

  3

>>> d2.has_key('ham') # Key membership test (1 means true)

1 >>> 'ham' in d3 # Key membership test alternative

  Dictionaries are mutable, so you can change, expand, and shrink them in-place without making new dictionaries, just like lists. Simply assign a value to a key to change or create the entry. The del statement works here too; it deletes the entry associated with the key specified as an index. Notice the nesting of a list inside a dictionary in this example (the value of key " ham "); all collection data types in Python can nest inside each other arbitrarily:

  >>> d2['ham'] = ['grill', 'bake', 'fry'] # Change entry. >>> d2 {'eggs': 3, 'spam': 2, 'ham': ['grill', 'bake', 'fry']} >>> del d2['eggs'] # Delete entry. >>> d2 respectively.

  >>> d2.values( ), d2.items( ) ([3, 1, 2], [('eggs', 3), ('ham', 1), ('spam', 2)])

  Such lists are useful in loops that need to step through dictionary entries one by one. Fetching a nonexistent key is normally an error, but the get method returns a default value ( None , or a passed-in default) if the key doesn't exist.

  

>>> d2.get('spam'), d2.get('toast'), d2.get('toast', 88)

(2, None, 88)

  The update method provides something similar to concatenation for dictionaries; it merges the keys and values of one dictionary into another, blindly overwiting values of the

  creator name by indexing on language name:

  >>> table = {'Python': 'Guido van Rossum', ... 'Perl': 'Larry Wall', ... 'Tcl': 'John Ousterhout' } ... >>> language = 'Python' >>> creator = table[language] >>> creator 'Guido van Rossum' >>> for lang in table.keys( ): ... print lang, '\t', table[lang] ... Tcl John Ousterhout Python Guido van Rossum

  membership to work on dictionaries as well.

6.4.5 Dictionary Usage Notes

  Here are a few additional details you should be aware of when using dictionaries:

  Sequence operations don't work. Dictionaries are

  mappings, not sequences; because there's no notion of ordering among their items, things like concatenation (an ordered joining) and slicing (extracting contiguous section) simply don't apply. In fact, Python raises an error when your code runs, if you try to do such things. When you use lists, it is illegal to assign to an offset that is off the end of the list:

  >>> L = [ ] >>> L[99] = 'spam' Traceback (most recent call last): File "<stdin>", line 1, in ? IndexError: list assignment index out of range

  Although you could use repetition to pre-allocate as big a list as you'll need (e.g., [0]*100 ), you can also do something that looks similar with dictionaries, which does not require such space allocations. By using integer keys, dictionaries can emulate lists that seem to grow on offset assignment:

  >>> D = { }

  >>> Matrix = { } >>> Matrix[(2,3,4)] = 88 >>> Matrix[(7,8,9)] = 99 >>> >>> X = 2; Y = 3; Z = 4 # ; separates statements. >>> Matrix[(X,Y,Z)]

  88 >>> Matrix {(2, 3, 4): 88, (7, 8, 9): 99}

  Here, we use a dictionary to represent a three-dimensional array, all of which are empty except for the two positions, and . The keys are tuples that record the

  (2,3,4) (7,8,8)

  coordinates of nonempty slots. Rather than allocating a large and mostly empty three-dimensional matrix, we can use a

  >>> try: ... print Matrix[(2,3,6)] # Try to index. ... except KeyError: # Catch and recover. ... print 0 ...

  >>> Matrix.get((2,3,4), 0) # Exists; fetch and return.

  88

>>> Matrix.get((2,3,6), 0) # Doesn't exist; use default arg.

  We'll study the if and try statements later.

6.4.5.3 Using dictionaries as "records"

  ... 'jobs': ['trainer', 'writer'], ... 'web': 'www.rmi.net/~lutz', ... 'home': {'state': 'CO', 'zip':80501}}

  This example uses a dictionary to capture object properties again, but has coded it all at once (rather than assigning to each key separately), and has nested a list and a dictionary to represent structure property values. To fetch components of nested objects, simply string together indexing operations:

  >>> mel['name'] 'Mark' >>> mel['jobs'] ['trainer', 'writer'] >>> mel['jobs'][1] 'writer'

  Why You Will Care: Dictionary Interfaces Besides being a convenient way to store information by key in your programs,

some Python extensions also present interfaces that look and work the same as

dictionaries. For instance, Python's interface to dbm access-by-key files looks

much like a dictionary that must be opened; strings are stored and fetched using

key indexes: import anydbm file = anydbm.open("filename") # Link to file. file['key'] = 'data' # Store data by key. data = file['key'] # Fetch data by key.

  Later, you'll see that we can store entire Python objects this way too, if we

replace anydbm in the above with shelve (shelves are access-by-key databases

of persistent Python objects). For Internet work, Python's CGI script support also

presents a dictionary-like interface; a call to cgi.FieldStorage yields a dictionary-like object, with one entry per input field on the client's web page:

6.3 Dictionaries

  Besides lists, dictionaries are perhaps the most flexible built-in data type in Python. If you think of lists as ordered collections of objects, dictionaries are unordered collections; their chief distinction is that items are stored and fetched in dictionaries by

  key, instead of positional offset.

  Being a built-in type, dictionaries can replace many of the searching algorithms and data structures you might have to implement manually in lower-level languagesindexing a dictionary is a very fast search operation. Dictionaries also sometimes do the work of records and symbol tables used in other languages, can represent sparse (mostly empty) data

  Variable length, heterogeneous, arbitrarily nestable

  Like lists, dictionaries can grow and shrink in place (without making a copy), they can contain objects of any type, and support nesting to any depth (they can contain lists, other dictionaries, and so on).

  Of the category mutable mapping Dictionaries can be changed in place by assigning to indexes, but don't support the sequence operations that work on strings and lists. Because dictionaries are empty dictionary is an empty set of braces, and dictionaries can be nested by writing one as a value inside another dictionary, or within a list or tuple.

  

[4] The same note about the relative rarity of literals applies here: dictionaries are often built up

by assigning to new keys at runtime, rather than writing literals. But see the following section on

changing dictionaries; lists and dictionaries are grown in different ways. Assignment to new keys

works for dictionaries, but fails for lists (lists are grown with append instead).

  

Table 6-2. Common dictionary literals and operations

Operation Interpretation

  D1 = { } Empty dictionary

  D2 = {'spam': 2, 'eggs': 3} Two-item dictionary

Chapter 14. Advanced Function Topics This chapter introduces a collection of more advanced function-

  related topics: the lambda expression, functional programming tools such as map and list comprehensions, generators, and more. Part of the art of using functions lies in the interfaces between them, so we will also explore some general function

   help you start coding the ideas you've read about.

  Chapter 21. Class Coding Details Did all of make sense? If not, don't worry; now that

  we've had a quick tour, we're going to dig a bit deeper and study the concepts we've introduced in further detail. This chapter takes a second pass, to formalize and expand on some of the class coding ideas introduced in

Chapter 10. while and for Loops In this chapter, we meet Python's two main looping

  constructsstatements that repeat an action over and over. The first of these, the while loop, provides a way to code general loops; the second, the for statement, is designed for stepping through the items in a sequence object and running a block of code for each item. There are other kinds of looping operations in Python, but the two statements covered here are the primary syntax provided for coding repeated actions. We'll also study a few unusual statements such as break and continue here, because they are

  Part VI: Classes and OOP In Part VI, we study the basics of object-oriented

programming (OOP), as well as the code you write to use

  OOP in Pythonthe class statement. As you'll see, OOP is an option in Python, but a good one: no other construct in the language supports code reuse to the degree that the

  class statement does. Especially in larger programs,

  OOP's notion of programming by customizing is a powerful paradigm to apply, and can cut development time substantially when used well.

  Chapter 13. Scopes and Arguments looked at basic function definition and calls. As we've seen, the basic function model is simple to use in Python. This chapter presents the details behind Python's scopesthe

  places where variables are defined, as well as argument passingthe way that objects are sent to functions as inputs.

Chapter 27. Common Tasks in Python At this point in the book, you have been exposed to a fairly

  complete survey of the more formal aspects of the language (the syntax, the data types, etc.). In this chapter, we'll "step out of the classroom" by looking at a set of basic computing tasks and examining how Python programmers typically solve them, hopefully helping you ground the theoretical knowledge with concrete results.

  Python programmers don't like to reinvent wheels when they already have access to nice, round wheels in their garage. Thus, the most important content in this chapter is the description of selected tools that make up the Python standard librarybuilt-in of the standard Python toolset. Three other O'Reilly books provide excellent additional information: the Python Pocket

  Reference, written by Mark Lutz, which covers the most

  important modules in the standard library, along with the syntax and built-in functions in compact form; Fredrik Lundh's

  

Python Standard Library, which takes on the formidable task of

  both providing additional documentation for each module in the standard library as well as providing an example program showing how to use each module; and finally, Alex Martelli's

  

Python in a Nutshell provides a thorough yeteminently readable

  and concise description of the language and standard library. As we'll see in , Python comes with tools that make self-learning easy as well. Just as we can't cover every standard module, the set of tasks covered in this chapter is necessarily limited. If you want more,

  Part IV: Functions In Part IV, we study the Python functiona package of code

  that can be called repeatedly, with different inputs and outputs each time. We've already been calling functions earlier in the book: open , to make a file object, for instance. Here, the emphasis will be on coding user- defined functions, which compute values, perform part of a program's overall logic, or otherwise wrap code for easy reuse.

  Chapter 20. Class Coding Basics Now that we've talked about OOP in the abstract, let's move on

  to the details of how this translates to actual code. In this chapter and in

Chapter 21 , we fill in the syntax details behind the class model in Python. If you've never been exposed to OOP in the past, classes can be

  somewhat complicated if taken in a single dose. To make class coding easier to absorb, we'll begin our detailed look at OOP by taking a first look at classes in action in this chapter. We'll expand on the details introduced here in later chapters of this part of the book; but in their basic form, Python classes are easy to understand.

Chapter 12. Function Basics In Part III , we looked at basic procedural statements in Python. Here, we'll move on to explore a set of additional statements

  that create functions of our own. In simple terms, a function is a device that groups a set of statements, so they can be run more than once in a program. Functions also let us specify parameters that serve as function inputs, and may differ each time a function's code is run. summarizes the primary function-related tools we'll study in this part of the book.

  

Table 12-1. Function-related statements and expressions

27.1 Exploring on Your Own

  Before digging into specific tasks, we should say a brief word about self-exploration. We have not been exhaustive in coverage of object attributes or module contents in order to focus on the most important aspects of the objects under discussion. If you're curious about what we've left out, you can look it up in the Library Reference, or you can poke around in the Python interactive interpreter, as shown in this section. The dir built-in function returns a list of all of the attributes of an object, and, along with the type built-in, provides a great way to learn about the objects you're manipulating. For example:

  Using this new function on the same empty list yields:

  >>> mydir([ ]) # What are the attributes of lists?

['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

  You can then explore any Python object:

  >>> mydir(( )) # What are the attributes of tuples? [ ] # Note: no "normal" attributes >>> import sys # What are the attributes of files? >>> mydir(sys.stdin) # What are the attributes of files?

['close', 'closed', 'fileno', 'flush', 'isatty', 'mode', 'name', 'read', 'readinto',

'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines'] >>> mydir(sys) # Modules are objects too. ['argv', 'builtin_module_names', 'byteorder', 'copyright', 'displayhook',

  DESCRIPTION

This module provides access to some objects used or maintained by the

interpreter and to functions that interact strongly with the interpreter.

  Dynamic objects:

argv--command line arguments; argv[0] is the script pathname if known

path--module search path; path[0] is the script directory, else '' modules--dictionary of loaded modules displayhook--called to show results in an interactive session excepthook--called to handle any uncaught exception other than SystemExit

  DESCRIPTION This module provides socket operations and some related functions. On Unix, it supports IP (Internet Protocol) and Unix domain sockets. On other systems, it only supports IP. Functions specific for a socket are available as methods of the socket object.

  Functions: socket( ) -- create a new socket object fromfd( ) -- create a socket object from an open file descriptor [*] gethostname( ) -- return the current hostname gethostbyname( ) -- map a hostname to its IP number gethostbyaddr( ) -- map an IP number or hostname to DNS info

getservbyname( ) -- map a service name and a protocol name to a port

...

  

BINARY EXPRESSIONS NONE SPECIALMETHODS

BITWISE FILES NUMBERMETHODS STRINGMETHODS

BOOLEAN FLOAT NUMBERS STRINGS CALLABLEMETHODS FORMATTING OBJECTS SUBSCRIPTS CALLS FRAMEOBJECTS OPERATORS TRACEBACKS CLASSES FRAMES PACKAGES TRUTHVALUE

CODEOBJECTS FUNCTIONS POWER TUPLELITERALS

COERCIONS IDENTIFIERS PRECEDENCE TUPLES COMPARISON IMPORTING PRINTING TYPEOBJECTS COMPLEX INTEGER PRIVATENAMES TYPES CONDITIONAL LISTLITERALS RETURNING UNARY CONVERSIONS LISTS SCOPING UNICODE help> TYPES

3.2 The standard type hierarchy

  Part V: Modules Part V explores Python modules. Modules are packages of

  names that usually correspond to source files and serve as libraries of tools for use in other files and programs. We introduced modules very early (in

   as a way to

  retain and run code. Here, we fill in the remaining details on this subject, and study some advanced module-related topics, such as package (directory) imports.

  Part III: Statements and Syntax In Part III, we study Python's procedural statement set:

  statements that select from alternative actions, repeat operations, print objects, and so on. Since this is our first formal look at statements, we will also explore Python's general syntax model. As we'll see, Python has a familiar and simple syntax model, though we often type much less in Python statements than in some other languages.

  We'll also meet the boolean expressions in conjunction with conditional statements and loops, and learn about Python documentation schemes while studying the syntax of documentation strings and comments. At an abstract

  Part I: Getting Started Part I begins by exploring some of the ideas behind Python, the Python execution model, and ways to launch

  program code. We won't actually start writing code until the next part, but make sure you have at least a basic understanding of Python's design goals and program launch techniques covered here before moving ahead to language details.

  Part II: Types and Operations In Part II, we study Python's built-in core data types,

  sometimes called object types. Although there are more kinds of objects in Python than we will meet in this part, the types discussed here are generally considered the core data typesthe main subjects of almost every Python script you're likely to read or write. This part of the book is organized around major core types, but watch for related topics, such as dynamic typing and general object categories, to also appear along the way. Because chapters in this part lay the groundwork assumed by later chapters, this part will work best if read