Python: List Processing, Comprehensions, Variables, and Type Checking
Advertisement
This Python programming tutorial covers list processing, list comprehensions, variables, objects, type checking, and related concepts.
List Processing
Working with lists is very common, and Python is very adept at processing them. We’ve already seen many examples:
>>> a = [1, 2, 3, 4, 5]
>>> sum(a)
15
>>> a[0:3]
[1, 2, 3]
>>> a * 2
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
>>> min(a)
1
List Comprehensions
List comprehensions create a new list by applying an operation to each element of a sequence.
>>> a = [1,2,3,4,5]
>>> b = [2*x for x in a]
>>> b
[2,4,6,8,10]
A list comprehension can also filter elements based on a condition:
>>> a = [1, -5, 4, 2, -2, 10]
>>> b = [2*x for x in a if x > 0]
>>> b
[2,8,4,20]
General syntax:
[expression for x in s if condition]
What it means:
result = []
for x in s:
if condition:
result.append(expression)
List comprehensions can be used anywhere a sequence is expected.
>>> a = [1,2,3,4]
>>> sum([x*x for x in a])
30
List Comprehension Examples
List comprehensions are hugely useful:
-
Collecting the values of a specific field:
stocknames = [s['name'] for s in stocks] -
Performing database-like queries:
a = [s for s in stocks if s['price'] > 100 and s['shares'] > 50 ]
Variable Assignment
Variables in Python are names for values. A variable name does not represent a fixed memory location into which values are stored (like in C, C++, Fortran, etc.). Assignment is just a naming operation.
Variables and Values
At any time, a variable can be redefined to new value.
a = 42
...
a = "Hello"
Variables are not restricted to one data type.
Names, Values, Types
Names do not have a “type” — it’s just a name. However, values do have an underlying type.
>>> a = 42
>>> b = "Hello World"
>>> type(a)
<type 'int'>
>>> type(b)
<type 'str'>
The type() function will tell you the type of a value. The type name is usually a function that creates or converts a value to that type.
Reference Counting
Variable assignment never copies anything! Instead, it just updates a reference count.
a = 42
b = a
c = [1,2]
c.append(b)
So, different variables might be referring to the same object (check with the is operator).
>>> a is b
True
Reassignment never overwrites memory, so you normally don’t notice any of the sharing. Instead, it just updates a reference count.
a = 42
b = a
a = 37
When you reassign a variable, the name is just made to point to the new value. “Copying” mutable objects such as lists and dicts is a hidden danger as explained below.
>>> a = [1,2,3,4]
>>> b = a
>>> b[2] = -10
>>> a
[1, 2, -10, 4]
Changes affect both variables! Reason: Different variable names are referring to exactly the same object.
Making a Copy
You have to take special steps to copy data.
>>> a = [2,3,[100,101],4]
>>> b = list(a) # Make a copy
>>> a is b
False
Sometimes you need to make a copy of an object and all objects contained within it. Use the copy module.
>>> a = [2,3,[100,101],4]
>>> import copy
>>> b = copy.deepcopy(a)
>>> a[2].append(102)
>>> b[2]
[100,101]
Everything is an Object
Numbers, strings, lists, functions, exceptions, classes, instances, etc… All objects are said to be “first-class”.
Meaning: All objects that can be named can be passed around as data, placed in containers, etc., without any restrictions. There are no “special” kinds of objects.
First Class Objects
A simple example:
>>> import math
>>> items = [abs, math, ValueError ]
>>> items
[<built-in function abs>, <module 'math' from '/usr/lib/python2.7/lib-dynload/math.so'>, <type 'exceptions.ValueError'>]
>>> items[0](-45)
45
Type Checking
How to tell if an object is a specific type:
if type(a) is list:
print "a is a list"
if isinstance(a,list): # Preferred
print "a is a list"
Checking for one of many types:
if isinstance(a,(list,tuple)):
print "a is a list or tuple"
Advertisement