Home site for the Start Programming with Python ebook.
Learn basic and intermediate programming skills in an easy-to-learn and fun-to-use language. Many of the ideas you will learn will carry over to other programming languages and ideas.

Facebook SDK

Tuesday, April 1, 2014

Beginning Python Tutorial (Part 5)

Python Dictionaries

Next to lists, dictionaries are one of the most useful data types in Python. Python dictionaries are unordered collections of objects, typed to a keyword. Python lists, on the other hand, are ordered collections that use an offset. Because of their construction, dictionaries can replace many "typical" search algorithms and data structures found in C and related languages.

Dictionaries include the following properties:

Accessed by keyword, not an offset. Dictionaries are similar to associative arrays.

Stored objects are in a random order to provide faster lookup. When created, a dictionary stores items in any order it chooses. To get a value, simply supply the key.

They are variable length, can hold objects of any type (including other dictionaries), and support deep nesting.

They are mutable but can't be modified like lists or strings; they are the only data type that supports mapping.

Basic operations

As previously stated, you create dictionaries and access items via a key. The len() function can be used to give the number of items stored in a dictionary or the length of the key list. The keys() method returns all the keys in the dictionary as a list. Here's a few examples:

Since dictionaries are mutable, you can add and delete values to them without creating a new dictionary object. Just assign a value to a key to change or create an entry and use del to delete an object associated with a given key.

To compare with lists, when adding a new object to a dictionary only requires making a new keyword and value. Lists will return an "index out-of-bounds" type error if the offset is past the end of the list. Therefore you must use append or slicing to add values to lists.

Here is a more realistic dictionary example. The following example creates a table that maps programming language names (the keys) to their creators (the values). You fetch a creator name by indexing on language name:

From this example, you might notice that the last command is similar to string and list iteration using the forcommand. However, you'll also notice that, since dictionaries aren't sequences, you can't use the standard forstatement. You must use the keys() method to return a list of all the keywords which you can then iterate through like a normal list.

Dictionary details

Sequence operations don't work.As previously stated, dictionaries are mappings, not sequences. Because there's no order to dictionary items, functions like concatenation and slicing don't work.

Assigning to new indexes adds entriesKeys can be created when making a dictionary constant (i.e. when you initially create the dictionary) or by adding new values to an existing dictionary. The process is similar and the end result is the same.

Keys can be anything immutableThe previous examples showed keys as string objects, but any non-mutable object (like lists) can be used for a keyword. Numbers can be used to create a list-like object but without the ordering. Tuples (covered later) are sometimes used to make compound keys; class instances designed not to change can also be used if needed.

Well, we're nearly done with Python types. The next tutorial will cover tuples, which are basically immutable lists. The final topic for these initial tutorials will be files, which should be fairly easy for most people.