Home arrow Python arrow The Dictionary Python Object Type

The Dictionary Python Object Type

In this third part of a four-part series on Python object types, we will wrap up our discussion of lists and introduce you to some remarkable things you can do with dictionaries. This article is excerpted from chapter four of the book Learning Python, Third Edition, written by Mark Lutz (O'Reilly, 2008; ISBN: 0596513984). Copyright © 2008 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

TABLE OF CONTENTS:
  1. The Dictionary Python Object Type
  2. Dictionaries
  3. Nesting Revisited
  4. Sorting Keys: for Loops
By: O'Reilly Media
Rating: starstarstarstarstar / 5
January 29, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

List Comprehensions

In addition to sequence operations and list methods, Python includes a more advanced operation known as a list comprehension expression, which turns out to be a powerful way to process structures like our matrix. Suppose, for instance, that we need to extract the second column of our sample matrix. It’s easy to grab rows by simple indexing because the matrix is stored by rows, but it’s almost as easy to get a column with a list comprehension:

  >>> col2 = [row[1] for row in M]     # Collect the items in column 2
 
>>> col2
  [2, 5, 8]
  >>> M                               # The matrix is unchanged
  [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

List comprehensions derive from set notation; they are a way to build a new list by running an expression on each item in a sequence, one at a time, from left to right. List comprehensions are coded in square brackets (to tip you off to the fact that they make a list), and are composed of an expression and a looping construct that share a variable name (row, here). The preceding list comprehension means basically what it says: “Give merow[1]for each row in matrixM, in a new list.” The result is a new list containing column 2 of the matrix.

List comprehensions can be more complex in practice:

  >>> [row[1] + 1 for row in M]       # Add 1 to each item in column 2
  [3, 6, 9]

  >>> [row[1] for row in M if
row[1] % 2 == 0]                    
# Filter out odd items
 
[2, 8]

The first operation here, for instance, adds 1 to each item as it is collected, and the second uses anifclause to filter odd numbers out of the result using the%modulus expression (remainder of division). List comprehensions make new lists of results, but can be used to iterate over any iterable object—here, for instance, we’ll use list comprehensions to step over a hardcoded list of coordinates, and a string:

  >>> diag = [M[i][i] for i
in [0, 1, 2]]
                       # Collect a diagonal from matrix
  >>> diag
  [1, 5, 9]
  >>> doubles = [c * 2 for c
in 'spam']
                          # Repeat characters in a string
  >>> doubles
  ['ss', 'pp', 'aa', 'mm']

List comprehensions are a bit too involved for me to say more about them here. The main point of this brief introduction is to illustrate that Python includes both simple and advanced tools in its arsenal. List comprehensions are an optional feature, but they tend to be handy in practice, and often provide a substantial processing speed advantage. They also work on any type that is a sequence in Python, as well as some types that are not. You’ll hear more about them later in this book.



 
 
>>> More Python Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: