0% found this document useful (0 votes)
8 views41 pages

CH 17-21

Uploaded by

Divya S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views41 pages

CH 17-21

Uploaded by

Divya S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Chapter 17: Arrays

Parameter Details
b Represents signed integer of size 1 byte
B Represents unsigned integer of size 1 byte
c Represents character of size 1 byte
u Represents unicode character of size 2 bytes
h Represents signed integer of size 2 bytes
H Represents unsigned integer of size 2 bytes
i Represents signed integer of size 2 bytes
I Represents unsigned integer of size 2 bytes
w Represents unicode character of size 4 bytes
l Represents signed integer of size 4 bytes
L Represents unsigned integer of size 4 bytes
f Represents floating point of size 4 bytes
d Represents floating point of size 8 bytes
"Arrays" in Python are not the arrays in conventional programming languages like C and Java, but closer to lists. A
list can be a collection of either homogeneous or heterogeneous elements, and may contain ints, strings or other
lists.

Section 17.1: Access individual elements through indexes


Individual elements can be accessed through indexes. Python arrays are zero-indexed. Here is an example:

my_array = array('i', [1,2,3,4,5])


print(my_array[1])
# 2
print(my_array[2])
# 3
print(my_array[0])
# 1

Section 17.2: Basic Introduction to Arrays


An array is a data structure that stores values of same data type. In Python, this is the main difference between
arrays and lists.

While python lists can contain values corresponding to different data types, arrays in python can only contain
values corresponding to same data type. In this tutorial, we will understand the Python arrays with few examples.

If you are new to Python, get started with the Python Introduction article.

To use arrays in python language, you need to import the standard array module. This is because array is not a
fundamental data type like strings, integer etc. Here is how you can import array module in python :

from array import *

Once you have imported the array module, you can declare an array. Here is how you do it:

arrayIdentifierName = array(typecode, [Initializers])

GoalKicker.com – Python® Notes for Professionals 102


In the declaration above, arrayIdentifierName is the name of array, typecode lets python know the type of array
and Initializers are the values with which array is initialized.

Typecodes are the codes that are used to define the type of array values or the type of array. The table in the
parameters section shows the possible values you can use when declaring an array and it's type.

Here is a real world example of python array declaration :

my_array = array('i',[1,2,3,4])

In the example above, typecode used is i. This typecode represents signed integer whose size is 2 bytes.

Here is a simple example of an array containing 5 integers

from array import *


my_array = array('i', [1,2,3,4,5])
for i in my_array:
print(i)
# 1
# 2
# 3
# 4
# 5

Section 17.3: Append any value to the array using append()


method
my_array = array('i', [1,2,3,4,5])
my_array.append(6)
# array('i', [1, 2, 3, 4, 5, 6])

Note that the value 6 was appended to the existing array values.

Section 17.4: Insert value in an array using insert() method


We can use the insert() method to insert a value at any index of the array. Here is an example :

my_array = array('i', [1,2,3,4,5])


my_array.insert(0,0)
#array('i', [0, 1, 2, 3, 4, 5])

In the above example, the value 0 was inserted at index 0. Note that the first argument is the index while second
argument is the value.

Section 17.5: Extend python array using extend() method


A python array can be extended with more than one value using extend() method. Here is an example :

my_array = array('i', [1,2,3,4,5])


my_extnd_array = array('i', [7,8,9,10])
my_array.extend(my_extnd_array)
# array('i', [1, 2, 3, 4, 5, 7, 8, 9, 10])

We see that the array my_array was extended with values from my_extnd_array.

GoalKicker.com – Python® Notes for Professionals 103


Section 17.6: Add items from list into array using fromlist()
method
Here is an example:

my_array = array('i', [1,2,3,4,5])


c=[11,12,13]
my_array.fromlist(c)
# array('i', [1, 2, 3, 4, 5, 11, 12, 13])

So we see that the values 11,12 and 13 were added from list c to my_array.

Section 17.7: Remove any array element using remove()


method
Here is an example :

my_array = array('i', [1,2,3,4,5])


my_array.remove(4)
# array('i', [1, 2, 3, 5])

We see that the element 4 was removed from the array.

Section 17.8: Remove last array element using pop() method


pop removes the last element from the array. Here is an example :

my_array = array('i', [1,2,3,4,5])


my_array.pop()
# array('i', [1, 2, 3, 4])

So we see that the last element (5) was popped out of array.

Section 17.9: Fetch any element through its index using index()
method
index() returns first index of the matching value. Remember that arrays are zero-indexed.

my_array = array('i', [1,2,3,4,5])


print(my_array.index(5))
# 5
my_array = array('i', [1,2,3,3,5])
print(my_array.index(3))
# 3

Note in that second example that only one index was returned, even though the value exists twice in the array

Section 17.10: Reverse a python array using reverse() method


The reverse() method does what the name says it will do - reverses the array. Here is an example :

my_array = array('i', [1,2,3,4,5])


my_array.reverse()
# array('i', [5, 4, 3, 2, 1])

GoalKicker.com – Python® Notes for Professionals 104


Section 17.11: Get array buer information through
buer_info() method
This method provides you the array buffer start address in memory and number of elements in array. Here is an
example:

my_array = array('i', [1,2,3,4,5])


my_array.buffer_info()
(33881712, 5)

Section 17.12: Check for number of occurrences of an element


using count() method
count() will return the number of times and element appears in an array. In the following example we see that the
value 3 occurs twice.

my_array = array('i', [1,2,3,3,5])


my_array.count(3)
# 2

Section 17.13: Convert array to string using tostring() method


tostring() converts the array to a string.

my_char_array = array('c', ['g','e','e','k'])


# array('c', 'geek')
print(my_char_array.tostring())
# geek

Section 17.14: Convert array to a python list with same


elements using tolist() method
When you need a Python list object, you can utilize the tolist() method to convert your array to a list.

my_array = array('i', [1,2,3,4,5])


c = my_array.tolist()
# [1, 2, 3, 4, 5]

Section 17.15: Append a string to char array using fromstring()


method
You are able to append a string to a character array using fromstring()

my_char_array = array('c', ['g','e','e','k'])


my_char_array.fromstring("stuff")
print(my_char_array)
#array('c', 'geekstuff')

GoalKicker.com – Python® Notes for Professionals 105


Chapter 18: Multidimensional arrays
Section 18.1: Lists in lists
A good way to visualize a 2d array is as a list of lists. Something like this:

lst=[[1,2,3],[4,5,6],[7,8,9]]

here the outer list lst has three things in it. each of those things is another list: The first one is: [1,2,3], the second
one is: [4,5,6] and the third one is: [7,8,9]. You can access these lists the same way you would access another
other element of a list, like this:

print (lst[0])
#output: [1, 2, 3]

print (lst[1])
#output: [4, 5, 6]

print (lst[2])
#output: [7, 8, 9]

You can then access the different elements in each of those lists the same way:

print (lst[0][0])
#output: 1

print (lst[0][1])
#output: 2

Here the first number inside the [] brackets means get the list in that position. In the above example we used the
number 0 to mean get the list in the 0th position which is [1,2,3]. The second set of [] brackets means get the
item in that position from the inner list. In this case we used both 0 and 1 the 0th position in the list we got is the
number 1 and in the 1st position it is 2

You can also set values inside these lists the same way:

lst[0]=[10,11,12]

Now the list is [[10,11,12],[4,5,6],[7,8,9]]. In this example we changed the whole first list to be a completely
new list.

lst[1][2]=15

Now the list is [[10,11,12],[4,5,15],[7,8,9]]. In this example we changed a single element inside of one of the
inner lists. First we went into the list at position 1 and changed the element within it at position 2, which was 6 now
it's 15.

Section 18.2: Lists in lists in lists in..


This behaviour can be extended. Here is a 3-dimensional array:

[[[111,112,113],[121,122,123],[131,132,133]],[[211,212,213],[221,222,223],[231,232,233]],[[311,312,
313],[321,322,323],[331,332,333]]]

GoalKicker.com – Python® Notes for Professionals 106


As is probably obvious, this gets a bit hard to read. Use backslashes to break up the different dimensions:

[[[111,112,113],[121,122,123],[131,132,133]],\
[[211,212,213],[221,222,223],[231,232,233]],\
[[311,312,313],[321,322,323],[331,332,333]]]

By nesting the lists like this, you can extend to arbitrarily high dimensions.

Accessing is similar to 2D arrays:

print(myarray)
print(myarray[1])
print(myarray[2][1])
print(myarray[1][0][2])
etc.

And editing is also similar:

myarray[1]=new_n-1_d_list
myarray[2][1]=new_n-2_d_list
myarray[1][0][2]=new_n-3_d_list #or a single number if you're dealing with 3D arrays
etc.

GoalKicker.com – Python® Notes for Professionals 107


Chapter 19: Dictionary
Parameter Details
key The desired key to lookup
value The value to set or return

Section 19.1: Introduction to Dictionary


A dictionary is an example of a key value store also known as Mapping in Python. It allows you to store and retrieve
elements by referencing a key. As dictionaries are referenced by key, they have very fast lookups. As they are
primarily used for referencing items by key, they are not sorted.

creating a dict

Dictionaries can be initiated in many ways:

literal syntax
d = {} # empty dict
d = {'key': 'value'} # dict with initial values

Python 3.x Version ≥ 3.5

# Also unpacking one or multiple dictionaries with the literal syntax is possible

# makes a shallow copy of otherdict


d = {**otherdict}
# also updates the shallow copy with the contents of the yetanotherdict.
d = {**otherdict, **yetanotherdict}

dict comprehension
d = {k:v for k,v in [('key', 'value',)]}

see also: Comprehensions

built-in class: dict()


d = dict() # empty dict
d = dict(key='value') # explicit keyword arguments
d = dict([('key', 'value')]) # passing in a list of key/value pairs
# make a shallow copy of another dict (only possible if keys are only strings!)
d = dict(**otherdict)

modifying a dict

To add items to a dictionary, simply create a new key with a value:

d['newkey'] = 42

It also possible to add list and dictionary as value:

d['new_list'] = [1, 2, 3]
d['new_dict'] = {'nested_dict': 1}

To delete an item, delete the key from the dictionary:

del d['newkey']

GoalKicker.com – Python® Notes for Professionals 108


Section 19.2: Avoiding KeyError Exceptions
One common pitfall when using dictionaries is to access a non-existent key. This typically results in a KeyError
exception

mydict = {}
mydict['not there']

Traceback (most recent call last):


File "<stdin>", line 1, in <module>
KeyError: 'not there'

One way to avoid key errors is to use the dict.get method, which allows you to specify a default value to return in
the case of an absent key.

value = mydict.get(key, default_value)

Which returns mydict[key] if it exists, but otherwise returns default_value. Note that this doesn't add key to
mydict. So if you want to retain that key value pair, you should use mydict.setdefault(key, default_value),
which does store the key value pair.

mydict = {}
print(mydict)
# {}
print(mydict.get("foo", "bar"))
# bar
print(mydict)
# {}
print(mydict.setdefault("foo", "bar"))
# bar
print(mydict)
# {'foo': 'bar'}

An alternative way to deal with the problem is catching the exception

try:
value = mydict[key]
except KeyError:
value = default_value

You could also check if the key is in the dictionary.

if key in mydict:
value = mydict[key]
else:
value = default_value

Do note, however, that in multi-threaded environments it is possible for the key to be removed from the dictionary
after you check, creating a race condition where the exception can still be thrown.

Another option is to use a subclass of dict, collections.defaultdict, that has a default_factory to create new entries in
the dict when given a new_key.

Section 19.3: Iterating Over a Dictionary


If you use a dictionary as an iterator (e.g. in a for statement), it traverses the keys of the dictionary. For example:

GoalKicker.com – Python® Notes for Professionals 109


d = {'a': 1, 'b': 2, 'c':3}
for key in d:
print(key, d[key])
# c 3
# b 2
# a 1

The same is true when used in a comprehension

print([key for key in d])


# ['c', 'b', 'a']

Python 3.x Version ≥ 3.0

The items() method can be used to loop over both the key and value simultaneously:

for key, value in d.items():


print(key, value)
# c 3
# b 2
# a 1

While the values() method can be used to iterate over only the values, as would be expected:

for key, value in d.values():


print(key, value)
# 3
# 2
# 1

Python 2.x Version ≥ 2.2

Here, the methods keys(), values() and items() return lists, and there are the three extra methods iterkeys()
itervalues() and iteritems() to return iterators.

Section 19.4: Dictionary with default values


Available in the standard library as defaultdict

from collections import defaultdict

d = defaultdict(int)
d['key'] # 0
d['key'] = 5
d['key'] # 5

d = defaultdict(lambda: 'empty')
d['key'] # 'empty'
d['key'] = 'full'
d['key'] # 'full'

[*] Alternatively, if you must use the built-in dict class, using dict.setdefault() will allow you to create a default
whenever you access a key that did not exist before:

>>> d = {}
{}
>>> d.setdefault('Another_key', []).append("This worked!")
>>> d

GoalKicker.com – Python® Notes for Professionals 110


{'Another_key': ['This worked!']}

Keep in mind that if you have many values to add, dict.setdefault() will create a new instance of the initial value
(in this example a []) every time it's called - which may create unnecessary workloads.

[*] Python Cookbook, 3rd edition, by David Beazley and Brian K. Jones (O’Reilly). Copyright 2013 David Beazley and Brian
Jones, 978-1-449-34037-7.

Section 19.5: Merging dictionaries


Consider the following dictionaries:

>>> fish = {'name': "Nemo", 'hands': "fins", 'special': "gills"}


>>> dog = {'name': "Clifford", 'hands': "paws", 'color': "red"}

Python 3.5+
>>> fishdog = {**fish, **dog}
>>> fishdog
{'hands': 'paws', 'color': 'red', 'name': 'Clifford', 'special': 'gills'}

As this example demonstrates, duplicate keys map to their lattermost value (for example "Clifford" overrides
"Nemo").

Python 3.3+
>>> from collections import ChainMap
>>> dict(ChainMap(fish, dog))
{'hands': 'fins', 'color': 'red', 'special': 'gills', 'name': 'Nemo'}

With this technique the foremost value takes precedence for a given key rather than the last ("Clifford" is thrown
out in favor of "Nemo").

Python 2.x, 3.x


>>> from itertools import chain
>>> dict(chain(fish.items(), dog.items()))
{'hands': 'paws', 'color': 'red', 'name': 'Clifford', 'special': 'gills'}

This uses the lattermost value, as with the **-based technique for merging ("Clifford" overrides "Nemo").

>>> fish.update(dog)
>>> fish
{'color': 'red', 'hands': 'paws', 'name': 'Clifford', 'special': 'gills'}

dict.update uses the latter dict to overwrite the previous one.

Section 19.6: Accessing keys and values


When working with dictionaries, it's often necessary to access all the keys and values in the dictionary, either in a
for loop, a list comprehension, or just as a plain list.

Given a dictionary like:

mydict = {
'a': '1',

GoalKicker.com – Python® Notes for Professionals 111


'b': '2'
}

You can get a list of keys using the keys() method:

print(mydict.keys())
# Python2: ['a', 'b']
# Python3: dict_keys(['b', 'a'])

If instead you want a list of values, use the values() method:

print(mydict.values())
# Python2: ['1', '2']
# Python3: dict_values(['2', '1'])

If you want to work with both the key and its corresponding value, you can use the items() method:

print(mydict.items())
# Python2: [('a', '1'), ('b', '2')]
# Python3: dict_items([('b', '2'), ('a', '1')])

NOTE: Because a dict is unsorted, keys(), values(), and items() have no sort order. Use sort(), sorted(), or an
OrderedDict if you care about the order that these methods return.

Python 2/3 Difference: In Python 3, these methods return special iterable objects, not lists, and are the equivalent
of the Python 2 iterkeys(), itervalues(), and iteritems() methods. These objects can be used like lists for the
most part, though there are some differences. See PEP 3106 for more details.

Section 19.7: Accessing values of a dictionary


dictionary = {"Hello": 1234, "World": 5678}
print(dictionary["Hello"])

The above code will print 1234.

The string "Hello" in this example is called a key. It is used to lookup a value in the dict by placing the key in
square brackets.

The number 1234 is seen after the respective colon in the dict definition. This is called the value that "Hello" maps
to in this dict.

Looking up a value like this with a key that does not exist will raise a KeyError exception, halting execution if
uncaught. If we want to access a value without risking a KeyError, we can use the dictionary.get method. By
default if the key does not exist, the method will return None. We can pass it a second value to return instead of
None in the event of a failed lookup.

w = dictionary.get("whatever")
x = dictionary.get("whatever", "nuh-uh")

In this example w will get the value None and x will get the value "nuh-uh".

Section 19.8: Creating a dictionary


Rules for creating a dictionary:

GoalKicker.com – Python® Notes for Professionals 112


Every key must be unique (otherwise it will be overridden)
Every key must be hashable (can use the hash function to hash it; otherwise TypeError will be thrown)
There is no particular order for the keys.

# Creating and populating it with values


stock = {'eggs': 5, 'milk': 2}

# Or creating an empty dictionary


dictionary = {}

# And populating it after


dictionary['eggs'] = 5
dictionary['milk'] = 2

# Values can also be lists


mydict = {'a': [1, 2, 3], 'b': ['one', 'two', 'three']}

# Use list.append() method to add new elements to the values list


mydict['a'].append(4) # => {'a': [1, 2, 3, 4], 'b': ['one', 'two', 'three']}
mydict['b'].append('four') # => {'a': [1, 2, 3, 4], 'b': ['one', 'two', 'three', 'four']}

# We can also create a dictionary using a list of two-items tuples


iterable = [('eggs', 5), ('milk', 2)]
dictionary = dict(iterables)

# Or using keyword argument:


dictionary = dict(eggs=5, milk=2)

# Another way will be to use the dict.fromkeys:


dictionary = dict.fromkeys((milk, eggs)) # => {'milk': None, 'eggs': None}
dictionary = dict.fromkeys((milk, eggs), (2, 5)) # => {'milk': 2, 'eggs': 5}

Section 19.9: Creating an ordered dictionary


You can create an ordered dictionary which will follow a determined order when iterating over the keys in the
dictionary.

Use OrderedDict from the collections module. This will always return the dictionary elements in the original
insertion order when iterated over.

from collections import OrderedDict

d = OrderedDict()
d['first'] = 1
d['second'] = 2
d['third'] = 3
d['last'] = 4

# Outputs "first 1", "second 2", "third 3", "last 4"


for key in d:
print(key, d[key])

Section 19.10: Unpacking dictionaries using the ** operator


You can use the ** keyword argument unpacking operator to deliver the key-value pairs in a dictionary into a
function's arguments. A simplified example from the official documentation:

>>>

GoalKicker.com – Python® Notes for Professionals 113


>>> def parrot(voltage, state, action):
... print("This parrot wouldn't", action, end=' ')
... print("if you put", voltage, "volts through it.", end=' ')
... print("E's", state, "!")
...
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)

This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !

As of Python 3.5 you can also use this syntax to merge an arbitrary number of dict objects.

>>> fish = {'name': "Nemo", 'hands': "fins", 'special': "gills"}


>>> dog = {'name': "Clifford", 'hands': "paws", 'color': "red"}
>>> fishdog = {**fish, **dog}
>>> fishdog

{'hands': 'paws', 'color': 'red', 'name': 'Clifford', 'special': 'gills'}

As this example demonstrates, duplicate keys map to their lattermost value (for example "Clifford" overrides
"Nemo").

Section 19.11: The trailing comma


Like lists and tuples, you can include a trailing comma in your dictionary.

role = {"By day": "A typical programmer",


"By night": "Still a typical programmer", }

PEP 8 dictates that you should leave a space between the trailing comma and the closing brace.

Section 19.12: The dict() constructor


The dict() constructor can be used to create dictionaries from keyword arguments, or from a single iterable of
key-value pairs, or from a single dictionary and keyword arguments.

dict(a=1, b=2, c=3) # {'a': 1, 'b': 2, 'c': 3}


dict([('d', 4), ('e', 5), ('f', 6)]) # {'d': 4, 'e': 5, 'f': 6}
dict([('a', 1)], b=2, c=3) # {'a': 1, 'b': 2, 'c': 3}
dict({'a' : 1, 'b' : 2}, c=3) # {'a': 1, 'b': 2, 'c': 3}

Section 19.13: Dictionaries Example


Dictionaries map keys to values.

car = {}
car["wheels"] = 4
car["color"] = "Red"
car["model"] = "Corvette"

Dictionary values can be accessed by their keys.

print "Little " + car["color"] + " " + car["model"] + "!"


# This would print out "Little Red Corvette!"

Dictionaries can also be created in a JSON style:

GoalKicker.com – Python® Notes for Professionals 114


car = {"wheels": 4, "color": "Red", "model": "Corvette"}

Dictionary values can be iterated over:

for key in car:


print key + ": " + car[key]

# wheels: 4
# color: Red
# model: Corvette

Section 19.14: All combinations of dictionary values


options = {
"x": ["a", "b"],
"y": [10, 20, 30]
}

Given a dictionary such as the one shown above, where there is a list representing a set of values to explore for the
corresponding key. Suppose you want to explore "x"="a" with "y"=10, then "x"="a" with"y"=10, and so on until
you have explored all possible combinations.

You can create a list that returns all such combinations of values using the following code.

import itertools

options = {
"x": ["a", "b"],
"y": [10, 20, 30]}

keys = options.keys()
values = (options[key] for key in keys)
combinations = [dict(zip(keys, combination)) for combination in itertools.product(*values)]
print combinations

This gives us the following list stored in the variable combinations:

[{'x': 'a', 'y': 10},


{'x': 'b', 'y': 10},
{'x': 'a', 'y': 20},
{'x': 'b', 'y': 20},
{'x': 'a', 'y': 30},
{'x': 'b', 'y': 30}]

GoalKicker.com – Python® Notes for Professionals 115


Chapter 20: List
The Python List is a general data structure widely used in Python programs. They are found in other languages,
often referred to as dynamic arrays. They are both mutable and a sequence data type that allows them to be indexed
and sliced. The list can contain different types of objects, including other list objects.

Section 20.1: List methods and supported operators


Starting with a given list a:

a = [1, 2, 3, 4, 5]

1. append(value) – appends a new element to the end of the list.

# Append values 6, 7, and 7 to the list


a.append(6)
a.append(7)
a.append(7)
# a: [1, 2, 3, 4, 5, 6, 7, 7]

# Append another list


b = [8, 9]
a.append(b)
# a: [1, 2, 3, 4, 5, 6, 7, 7, [8, 9]]

# Append an element of a different type, as list elements do not need to have the same type
my_string = "hello world"
a.append(my_string)
# a: [1, 2, 3, 4, 5, 6, 7, 7, [8, 9], "hello world"]

Note that the append() method only appends one new element to the end of the list. If you append a list to
another list, the list that you append becomes a single element at the end of the first list.

# Appending a list to another list


a = [1, 2, 3, 4, 5, 6, 7, 7]
b = [8, 9]
a.append(b)
# a: [1, 2, 3, 4, 5, 6, 7, 7, [8, 9]]
a[8]
# Returns: [8,9]

2. extend(enumerable) – extends the list by appending elements from another enumerable.

a = [1, 2, 3, 4, 5, 6, 7, 7]
b = [8, 9, 10]

# Extend list by appending all elements from b


a.extend(b)
# a: [1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10]

# Extend list with elements from a non-list enumerable:


a.extend(range(3))
# a: [1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10, 0, 1, 2]

Lists can also be concatenated with the + operator. Note that this does not modify any of the original lists:

GoalKicker.com – Python® Notes for Professionals 117


a = [1, 2, 3, 4, 5, 6] + [7, 7] + b
# a: [1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10]

3. index(value, [startIndex]) – gets the index of the first occurrence of the input value. If the input value is
not in the list a ValueError exception is raised. If a second argument is provided, the search is started at that
specified index.

a.index(7)
# Returns: 6

a.index(49) # ValueError, because 49 is not in a.

a.index(7, 7)
# Returns: 7

a.index(7, 8) # ValueError, because there is no 7 starting at index 8

4. insert(index, value) – inserts value just before the specified index. Thus after the insertion the new
element occupies position index.

a.insert(0, 0) # insert 0 at position 0


a.insert(2, 5) # insert 5 at position 2
# a: [0, 1, 5, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10]

5. pop([index]) – removes and returns the item at index. With no argument it removes and returns the last
element of the list.

a.pop(2)
# Returns: 5
# a: [0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10]
a.pop(8)
# Returns: 7
# a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# With no argument:
a.pop()
# Returns: 10
# a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

6. remove(value) – removes the first occurrence of the specified value. If the provided value cannot be found, a
ValueError is raised.

a.remove(0)
a.remove(9)
# a: [1, 2, 3, 4, 5, 6, 7, 8]
a.remove(10)
# ValueError, because 10 is not in a

7. reverse() – reverses the list in-place and returns None.

a.reverse()
# a: [8, 7, 6, 5, 4, 3, 2, 1]

There are also other ways of reversing a list.

GoalKicker.com – Python® Notes for Professionals 118


8. count(value) – counts the number of occurrences of some value in the list.

a.count(7)
# Returns: 2

9. sort() – sorts the list in numerical and lexicographical order and returns None.

a.sort()
# a = [1, 2, 3, 4, 5, 6, 7, 8]
# Sorts the list in numerical order

Lists can also be reversed when sorted using the reverse=True flag in the sort() method.

a.sort(reverse=True)
# a = [8, 7, 6, 5, 4, 3, 2, 1]

If you want to sort by attributes of items, you can use the key keyword argument:

import datetime

class Person(object):
def __init__(self, name, birthday, height):
self.name = name
self.birthday = birthday
self.height = height

def __repr__(self):
return self.name

l = [Person("John Cena", datetime.date(1992, 9, 12), 175),


Person("Chuck Norris", datetime.date(1990, 8, 28), 180),
Person("Jon Skeet", datetime.date(1991, 7, 6), 185)]

l.sort(key=lambda item: item.name)


# l: [Chuck Norris, John Cena, Jon Skeet]

l.sort(key=lambda item: item.birthday)


# l: [Chuck Norris, Jon Skeet, John Cena]

l.sort(key=lambda item: item.height)


# l: [John Cena, Chuck Norris, Jon Skeet]

In case of list of dicts the concept is the same:

import datetime

l = [{'name':'John Cena', 'birthday': datetime.date(1992, 9, 12),'height': 175},


{'name': 'Chuck Norris', 'birthday': datetime.date(1990, 8, 28),'height': 180},
{'name': 'Jon Skeet', 'birthday': datetime.date(1991, 7, 6), 'height': 185}]

l.sort(key=lambda item: item['name'])


# l: [Chuck Norris, John Cena, Jon Skeet]

l.sort(key=lambda item: item['birthday'])


# l: [Chuck Norris, Jon Skeet, John Cena]

l.sort(key=lambda item: item['height'])


# l: [John Cena, Chuck Norris, Jon Skeet]

GoalKicker.com – Python® Notes for Professionals 119


Sort by sub dict:

import datetime

l = [{'name':'John Cena', 'birthday': datetime.date(1992, 9, 12),'size': {'height': 175,


'weight': 100}},
{'name': 'Chuck Norris', 'birthday': datetime.date(1990, 8, 28),'size' : {'height': 180,
'weight': 90}},
{'name': 'Jon Skeet', 'birthday': datetime.date(1991, 7, 6), 'size': {'height': 185,
'weight': 110}}]

l.sort(key=lambda item: item['size']['height'])


# l: [John Cena, Chuck Norris, Jon Skeet]

Better way to sort using attrgetter and itemgetter

Lists can also be sorted using attrgetter and itemgetter functions from the operator module. These can help
improve readability and reusability. Here are some examples,

from operator import itemgetter,attrgetter

people = [{'name':'chandan','age':20,'salary':2000},
{'name':'chetan','age':18,'salary':5000},
{'name':'guru','age':30,'salary':3000}]
by_age = itemgetter('age')
by_salary = itemgetter('salary')

people.sort(key=by_age) #in-place sorting by age


people.sort(key=by_salary) #in-place sorting by salary

itemgetter can also be given an index. This is helpful if you want to sort based on indices of a tuple.

list_of_tuples = [(1,2), (3,4), (5,0)]


list_of_tuples.sort(key=itemgetter(1))
print(list_of_tuples) #[(5, 0), (1, 2), (3, 4)]

Use the attrgetter if you want to sort by attributes of an object,

persons = [Person("John Cena", datetime.date(1992, 9, 12), 175),


Person("Chuck Norris", datetime.date(1990, 8, 28), 180),
Person("Jon Skeet", datetime.date(1991, 7, 6), 185)] #reusing Person class from above
example

person.sort(key=attrgetter('name')) #sort by name


by_birthday = attrgetter('birthday')
person.sort(key=by_birthday) #sort by birthday

10. clear() – removes all items from the list

a.clear()
# a = []

11. Replication – multiplying an existing list by an integer will produce a larger list consisting of that many copies
of the original. This can be useful for example for list initialization:

b = ["blah"] * 3
# b = ["blah", "blah", "blah"]

GoalKicker.com – Python® Notes for Professionals 120


b = [1, 3, 5] * 5
# [1, 3, 5, 1, 3, 5, 1, 3, 5, 1, 3, 5, 1, 3, 5]

Take care doing this if your list contains references to objects (eg a list of lists), see Common Pitfalls - List
multiplication and common references.

12. Element deletion – it is possible to delete multiple elements in the list using the del keyword and slice
notation:

a = list(range(10))
del a[::2]
# a = [1, 3, 5, 7, 9]
del a[-1]
# a = [1, 3, 5, 7]
del a[:]
# a = []

13. Copying

The default assignment "=" assigns a reference of the original list to the new name. That is, the original name
and new name are both pointing to the same list object. Changes made through any of them will be reflected
in another. This is often not what you intended.

b = a
a.append(6)
# b: [1, 2, 3, 4, 5, 6]

If you want to create a copy of the list you have below options.

You can slice it:

new_list = old_list[:]

You can use the built in list() function:

new_list = list(old_list)

You can use generic copy.copy():

import copy
new_list = copy.copy(old_list) #inserts references to the objects found in the original.

This is a little slower than list() because it has to find out the datatype of old_list first.

If the list contains objects and you want to copy them as well, use generic copy.deepcopy():

import copy
new_list = copy.deepcopy(old_list) #inserts copies of the objects found in the original.

Obviously the slowest and most memory-needing method, but sometimes unavoidable.

Python 3.x Version ≥ 3.0

GoalKicker.com – Python® Notes for Professionals 121


copy() – Returns a shallow copy of the list

aa = a.copy()
# aa = [1, 2, 3, 4, 5]

Section 20.2: Accessing list values


Python lists are zero-indexed, and act like arrays in other languages.

lst = [1, 2, 3, 4]
lst[0] # 1
lst[1] # 2

Attempting to access an index outside the bounds of the list will raise an IndexError.

lst[4] # IndexError: list index out of range

Negative indices are interpreted as counting from the end of the list.

lst[-1] # 4
lst[-2] # 3
lst[-5] # IndexError: list index out of range

This is functionally equivalent to

lst[len(lst)-1] # 4

Lists allow to use slice notation as lst[start:end:step]. The output of the slice notation is a new list containing
elements from index start to end-1. If options are omitted start defaults to beginning of list, end to end of list and
step to 1:

lst[1:] # [2, 3, 4]
lst[:3] # [1, 2, 3]
lst[::2] # [1, 3]
lst[::-1] # [4, 3, 2, 1]
lst[-1:0:-1] # [4, 3, 2]
lst[5:8] # [] since starting index is greater than length of lst, returns empty list
lst[1:10] # [2, 3, 4] same as omitting ending index

With this in mind, you can print a reversed version of the list by calling

lst[::-1] # [4, 3, 2, 1]

When using step lengths of negative amounts, the starting index has to be greater than the ending index otherwise
the result will be an empty list.

lst[3:1:-1] # [4, 3]

Using negative step indices are equivalent to the following code:

reversed(lst)[0:2] # 0 = 1 -1
# 2 = 3 -1

The indices used are 1 less than those used in negative indexing and are reversed.

GoalKicker.com – Python® Notes for Professionals 122


Advanced slicing

When lists are sliced the __getitem__() method of the list object is called, with a slice object. Python has a builtin
slice method to generate slice objects. We can use this to store a slice and reuse it later like so,

data = 'chandan purohit 22 2000' #assuming data fields of fixed length


name_slice = slice(0,19)
age_slice = slice(19,21)
salary_slice = slice(22,None)

#now we can have more readable slices


print(data[name_slice]) #chandan purohit
print(data[age_slice]) #'22'
print(data[salary_slice]) #'2000'

This can be of great use by providing slicing functionality to our objects by overriding __getitem__ in our class.

Section 20.3: Checking if list is empty


The emptiness of a list is associated to the boolean False, so you don't have to check len(lst) == 0, but just lst
or not lst

lst = []
if not lst:
print("list is empty")

# Output: list is empty

Section 20.4: Iterating over a list


Python supports using a for loop directly on a list:

my_list = ['foo', 'bar', 'baz']


for item in my_list:
print(item)

# Output: foo
# Output: bar
# Output: baz

You can also get the position of each item at the same time:

for (index, item) in enumerate(my_list):


print('The item in position {} is: {}'.format(index, item))

# Output: The item in position 0 is: foo


# Output: The item in position 1 is: bar
# Output: The item in position 2 is: baz

The other way of iterating a list based on the index value:

for i in range(0,len(my_list)):
print(my_list[i])
#output:
>>>
foo
bar

GoalKicker.com – Python® Notes for Professionals 123


baz

Note that changing items in a list while iterating on it may have unexpected results:

for item in my_list:


if item == 'foo':
del my_list[0]
print(item)

# Output: foo
# Output: baz

In this last example, we deleted the first item at the first iteration, but that caused bar to be skipped.

Section 20.5: Checking whether an item is in a list


Python makes it very simple to check whether an item is in a list. Simply use the in operator.

lst = ['test', 'twest', 'tweast', 'treast']

'test' in lst
# Out: True

'toast' in lst
# Out: False

Note: the in operator on sets is asymptotically faster than on lists. If you need to use it many times on
potentially large lists, you may want to convert your list to a set, and test the presence of elements on
the set.

slst = set(lst)
'test' in slst
# Out: True

Section 20.6: Any and All


You can use all() to determine if all the values in an iterable evaluate to True

nums = [1, 1, 0, 1]
all(nums)
# False
chars = ['a', 'b', 'c', 'd']
all(chars)
# True

Likewise, any() determines if one or more values in an iterable evaluate to True

nums = [1, 1, 0, 1]
any(nums)
# True
vals = [None, None, None, False]
any(vals)
# False

While this example uses a list, it is important to note these built-ins work with any iterable, including generators.

GoalKicker.com – Python® Notes for Professionals 124


vals = [1, 2, 3, 4]
any(val > 12 for val in vals)
# False
any((val * 2) > 6 for val in vals)
# True

Section 20.7: Reversing list elements


You can use the reversed function which returns an iterator to the reversed list:

In [3]: rev = reversed(numbers)

In [4]: rev
Out[4]: [9, 8, 7, 6, 5, 4, 3, 2, 1]

Note that the list "numbers" remains unchanged by this operation, and remains in the same order it was originally.

To reverse in place, you can also use the reverse method.

You can also reverse a list (actually obtaining a copy, the original list is unaffected) by using the slicing syntax,
setting the third argument (the step) as -1:

In [1]: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

In [2]: numbers[::-1]
Out[2]: [9, 8, 7, 6, 5, 4, 3, 2, 1]

Section 20.8: Concatenate and Merge lists


1. The simplest way to concatenate list1 and list2:

merged = list1 + list2

2. zip returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument
sequences or iterables:

alist = ['a1', 'a2', 'a3']


blist = ['b1', 'b2', 'b3']

for a, b in zip(alist, blist):


print(a, b)

# Output:
# a1 b1
# a2 b2
# a3 b3

If the lists have different lengths then the result will include only as many elements as the shortest one:

alist = ['a1', 'a2', 'a3']


blist = ['b1', 'b2', 'b3', 'b4']
for a, b in zip(alist, blist):
print(a, b)

# Output:
# a1 b1

GoalKicker.com – Python® Notes for Professionals 125


# a2 b2
# a3 b3

alist = []
len(list(zip(alist, blist)))

# Output:
# 0

For padding lists of unequal length to the longest one with Nones use itertools.zip_longest
(itertools.izip_longest in Python 2)

alist = ['a1', 'a2', 'a3']


blist = ['b1']
clist = ['c1', 'c2', 'c3', 'c4']

for a,b,c in itertools.zip_longest(alist, blist, clist):


print(a, b, c)

# Output:
# a1 b1 c1
# a2 None c2
# a3 None c3
# None None c4

3. Insert to a specific index values:

alist = [123, 'xyz', 'zara', 'abc']


alist.insert(3, [2009])
print("Final List :", alist)

Output:

Final List : [123, 'xyz', 'zara', 2009, 'abc']

Section 20.9: Length of a list


Use len() to get the one-dimensional length of a list.

len(['one', 'two']) # returns 2

len(['one', [2, 3], 'four']) # returns 3, not 4

len() also works on strings, dictionaries, and other data structures similar to lists.

Note that len() is a built-in function, not a method of a list object.

Also note that the cost of len() is O(1), meaning it will take the same amount of time to get the length of a list
regardless of its length.

Section 20.10: Remove duplicate values in list


Removing duplicate values in a list can be done by converting the list to a set (that is an unordered collection of
distinct objects). If a list data structure is needed, then the set can be converted back to a list using the function
list():

GoalKicker.com – Python® Notes for Professionals 126


names = ["aixk", "duke", "edik", "tofp", "duke"]
list(set(names))
# Out: ['duke', 'tofp', 'aixk', 'edik']

Note that by converting a list to a set the original ordering is lost.

To preserve the order of the list one can use an OrderedDict

import collections
>>> collections.OrderedDict.fromkeys(names).keys()
# Out: ['aixk', 'duke', 'edik', 'tofp']

Section 20.11: Comparison of lists


It's possible to compare lists and other sequences lexicographically using comparison operators. Both operands
must be of the same type.

[1, 10, 100] < [2, 10, 100]


# True, because 1 < 2
[1, 10, 100] < [1, 10, 100]
# False, because the lists are equal
[1, 10, 100] <= [1, 10, 100]
# True, because the lists are equal
[1, 10, 100] < [1, 10, 101]
# True, because 100 < 101
[1, 10, 100] < [0, 10, 100]
# False, because 0 < 1

If one of the lists is contained at the start of the other, the shortest list wins.

[1, 10] < [1, 10, 100]


# True

Section 20.12: Accessing values in nested list


Starting with a three-dimensional list:

alist = [[[1,2],[3,4]], [[5,6,7],[8,9,10], [12, 13, 14]]]

Accessing items in the list:

print(alist[0][0][1])
#2
#Accesses second element in the first list in the first list

print(alist[1][1][2])
#10
#Accesses the third element in the second list in the second list

Performing support operations:

alist[0][0].append(11)
print(alist[0][0][2])
#11
#Appends 11 to the end of the first list in the first list

GoalKicker.com – Python® Notes for Professionals 127


Using nested for loops to print the list:

for row in alist: #One way to loop through nested lists


for col in row:
print(col)
#[1, 2, 11]
#[3, 4]
#[5, 6, 7]
#[8, 9, 10]
#[12, 13, 14]

Note that this operation can be used in a list comprehension or even as a generator to produce efficiencies, e.g.:

[col for row in alist for col in row]


#[[1, 2, 11], [3, 4], [5, 6, 7], [8, 9, 10], [12, 13, 14]]

Not all items in the outer lists have to be lists themselves:

alist[1].insert(2, 15)
#Inserts 15 into the third position in the second list

Another way to use nested for loops. The other way is better but I've needed to use this on occasion:

for row in range(len(alist)): #A less Pythonic way to loop through lists


for col in range(len(alist[row])):
print(alist[row][col])

#[1, 2, 11]
#[3, 4]
#[5, 6, 7]
#[8, 9, 10]
#15
#[12, 13, 14]

Using slices in nested list:

print(alist[1][1:])
#[[8, 9, 10], 15, [12, 13, 14]]
#Slices still work

The final list:

print(alist)
#[[[1, 2, 11], [3, 4]], [[5, 6, 7], [8, 9, 10], 15, [12, 13, 14]]]

Section 20.13: Initializing a List to a Fixed Number of Elements


For immutable elements (e.g. None, string literals etc.):

my_list = [None] * 10
my_list = ['test'] * 10

For mutable elements, the same construct will result in all elements of the list referring to the same object, for
example, for a set:

>>> my_list=[{1}] * 10

GoalKicker.com – Python® Notes for Professionals 128


>>> print(my_list)
[{1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}]
>>> my_list[0].add(2)
>>> print(my_list)
[{1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}]

Instead, to initialize the list with a fixed number of different mutable objects, use:

my_list=[{1} for _ in range(10)]

GoalKicker.com – Python® Notes for Professionals 129


Chapter 21: List comprehensions
List comprehensions in Python are concise, syntactic constructs. They can be utilized to generate lists from other
lists by applying functions to each element in the list. The following section explains and demonstrates the use of
these expressions.

Section 21.1: List Comprehensions


A list comprehension creates a new list by applying an expression to each element of an iterable. The most basic
form is:

[ <expression> for <element> in <iterable> ]

There's also an optional 'if' condition:

[ <expression> for <element> in <iterable> if <condition> ]

Each <element> in the <iterable> is plugged in to the <expression> if the (optional) <condition> evaluates to true
. All results are returned at once in the new list. Generator expressions are evaluated lazily, but list comprehensions
evaluate the entire iterator immediately - consuming memory proportional to the iterator's length.

To create a list of squared integers:

squares = [x * x for x in (1, 2, 3, 4)]


# squares: [1, 4, 9, 16]

The for expression sets x to each value in turn from (1, 2, 3, 4). The result of the expression x * x is appended
to an internal list. The internal list is assigned to the variable squares when completed.

Besides a speed increase (as explained here), a list comprehension is roughly equivalent to the following for-loop:

squares = []
for x in (1, 2, 3, 4):
squares.append(x * x)
# squares: [1, 4, 9, 16]

The expression applied to each element can be as complex as needed:

# Get a list of uppercase characters from a string


[s.upper() for s in "Hello World"]
# ['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D']

# Strip off any commas from the end of strings in a list


[w.strip(',') for w in ['these,', 'words,,', 'mostly', 'have,commas,']]
# ['these', 'words', 'mostly', 'have,commas']

# Organize letters in words more reasonably - in an alphabetical order


sentence = "Beautiful is better than ugly"
["".join(sorted(word, key = lambda x: x.lower())) for word in sentence.split()]
# ['aBefiltuu', 'is', 'beertt', 'ahnt', 'gluy']

else

else can be used in List comprehension constructs, but be careful regarding the syntax. The if/else clauses should

GoalKicker.com – Python® Notes for Professionals 130


be used before for loop, not after:

# create a list of characters in apple, replacing non vowels with '*'


# Ex - 'apple' --> ['a', '*', '*', '*' ,'e']

[x for x in 'apple' if x in 'aeiou' else '*']


#SyntaxError: invalid syntax

# When using if/else together use them before the loop


[x if x in 'aeiou' else '*' for x in 'apple']
#['a', '*', '*', '*', 'e']

Note this uses a different language construct, a conditional expression, which itself is not part of the
comprehension syntax. Whereas the if after the for…in is a part of list comprehensions and used to filter
elements from the source iterable.

Double Iteration

Order of double iteration [... for x in ... for y in ...] is either natural or counter-intuitive. The rule of
thumb is to follow an equivalent for loop:

def foo(i):
return i, i + 0.5

for i in range(3):
for x in foo(i):
yield str(x)

This becomes:

[str(x)
for i in range(3)
for x in foo(i)
]

This can be compressed into one line as [str(x) for i in range(3) for x in foo(i)]

In-place Mutation and Other Side Effects

Before using list comprehension, understand the difference between functions called for their side effects
(mutating, or in-place functions) which usually return None, and functions that return an interesting value.

Many functions (especially pure functions) simply take an object and return some object. An in-place function
modifies the existing object, which is called a side effect. Other examples include input and output operations such
as printing.

list.sort() sorts a list in-place (meaning that it modifies the original list) and returns the value None. Therefore, it
won't work as expected in a list comprehension:

[x.sort() for x in [[2, 1], [4, 3], [0, 1]]]


# [None, None, None]

Instead, sorted() returns a sorted list rather than sorting in-place:

GoalKicker.com – Python® Notes for Professionals 131


[sorted(x) for x in [[2, 1], [4, 3], [0, 1]]]
# [[1, 2], [3, 4], [0, 1]]

Using comprehensions for side-effects is possible, such as I/O or in-place functions. Yet a for loop is usually more
readable. While this works in Python 3:

[print(x) for x in (1, 2, 3)]

Instead use:

for x in (1, 2, 3):


print(x)

In some situations, side effect functions are suitable for list comprehension. random.randrange() has the side
effect of changing the state of the random number generator, but it also returns an interesting value. Additionally,
next() can be called on an iterator.

The following random value generator is not pure, yet makes sense as the random generator is reset every time the
expression is evaluated:

from random import randrange


[randrange(1, 7) for _ in range(10)]
# [2, 3, 2, 1, 1, 5, 2, 4, 3, 5]

Whitespace in list comprehensions

More complicated list comprehensions can reach an undesired length, or become less readable. Although less
common in examples, it is possible to break a list comprehension into multiple lines like so:

[
x for x
in 'foo'
if x not in 'bar'
]

Section 21.2: Conditional List Comprehensions


Given a list comprehension you can append one or more if conditions to filter values.

[<expression> for <element> in <iterable> if <condition>]

For each <element> in <iterable>; if <condition> evaluates to True, add <expression> (usually a function of
<element>) to the returned list.

For example, this can be used to extract only even numbers from a sequence of integers:

[x for x in range(10) if x % 2 == 0]
# Out: [0, 2, 4, 6, 8]

Live demo

The above code is equivalent to:

even_numbers = []

GoalKicker.com – Python® Notes for Professionals 132


for x in range(10):
if x % 2 == 0:
even_numbers.append(x)

print(even_numbers)
# Out: [0, 2, 4, 6, 8]

Also, a conditional list comprehension of the form [e for x in y if c] (where e and c are expressions in terms of
x) is equivalent to list(filter(lambda x: c, map(lambda x: e, y))).

Despite providing the same result, pay attention to the fact that the former example is almost 2x faster than the
latter one. For those who are curious, this is a nice explanation of the reason why.

Note that this is quite different from the ... if ... else ... conditional expression (sometimes known as a
ternary expression) that you can use for the <expression> part of the list comprehension. Consider the following
example:

[x if x % 2 == 0 else None for x in range(10)]


# Out: [0, None, 2, None, 4, None, 6, None, 8, None]

Live demo

Here the conditional expression isn't a filter, but rather an operator determining the value to be used for the list
items:

<value-if-condition-is-true> if <condition> else <value-if-condition-is-false>

This becomes more obvious if you combine it with other operators:

[2 * (x if x % 2 == 0 else -1) + 1 for x in range(10)]


# Out: [1, -1, 5, -1, 9, -1, 13, -1, 17, -1]

Live demo

If you are using Python 2.7, xrange may be better than range for several reasons as described in the xrange
documentation.

[2 * (x if x % 2 == 0 else -1) + 1 for x in xrange(10)]


# Out: [1, -1, 5, -1, 9, -1, 13, -1, 17, -1]

The above code is equivalent to:

numbers = []
for x in range(10):
if x % 2 == 0:
temp = x
else:
temp = -1
numbers.append(2 * temp + 1)
print(numbers)
# Out: [1, -1, 5, -1, 9, -1, 13, -1, 17, -1]

One can combine ternary expressions and if conditions. The ternary operator works on the filtered result:

[x if x > 2 else '*' for x in range(10) if x % 2 == 0]


# Out: ['*', '*', 4, 6, 8]

GoalKicker.com – Python® Notes for Professionals 133


The same couldn't have been achieved just by ternary operator only:

[x if (x > 2 and x % 2 == 0) else '*' for x in range(10)]


# Out:['*', '*', '*', '*', 4, '*', 6, '*', 8, '*']

See also: Filters, which often provide a sufficient alternative to conditional list comprehensions.

Section 21.3: Avoid repetitive and expensive operations using


conditional clause
Consider the below list comprehension:

>>> def f(x):


... import time
... time.sleep(.1) # Simulate expensive function
... return x**2

>>> [f(x) for x in range(1000) if f(x) > 10]


[16, 25, 36, ...]

This results in two calls to f(x) for 1,000 values of x: one call for generating the value and the other for checking the
if condition. If f(x) is a particularly expensive operation, this can have significant performance implications.
Worse, if calling f() has side effects, it can have surprising results.

Instead, you should evaluate the expensive operation only once for each value of x by generating an intermediate
iterable (generator expression) as follows:

>>> [v for v in (f(x) for x in range(1000)) if v > 10]


[16, 25, 36, ...]

Or, using the builtin map equivalent:

>>> [v for v in map(f, range(1000)) if v > 10]


[16, 25, 36, ...]

Another way that could result in a more readable code is to put the partial result (v in the previous example) in an
iterable (such as a list or a tuple) and then iterate over it. Since v will be the only element in the iterable, the result is
that we now have a reference to the output of our slow function computed only once:

>>> [v for x in range(1000) for v in [f(x)] if v > 10]


[16, 25, 36, ...]

However, in practice, the logic of code can be more complicated and it's important to keep it readable. In general, a
separate generator function is recommended over a complex one-liner:

>>> def process_prime_numbers(iterable):


... for x in iterable:
... if is_prime(x):
... yield f(x)
...
>>> [x for x in process_prime_numbers(range(1000)) if x > 10]
[11, 13, 17, 19, ...]

Another way to prevent computing f(x) multiple times is to use the @functools.lru_cache()(Python 3.2+)
decorator on f(x). This way since the output of f for the input x has already been computed once, the second

GoalKicker.com – Python® Notes for Professionals 134


function invocation of the original list comprehension will be as fast as a dictionary lookup. This approach uses
memoization to improve efficiency, which is comparable to using generator expressions.

Say you have to flatten a list

l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]

Some of the methods could be:

reduce(lambda x, y: x+y, l)

sum(l, [])

list(itertools.chain(*l))

However list comprehension would provide the best time complexity.

[item for sublist in l for item in sublist]

The shortcuts based on + (including the implied use in sum) are, of necessity, O(L^2) when there are L sublists -- as
the intermediate result list keeps getting longer, at each step a new intermediate result list object gets allocated,
and all the items in the previous intermediate result must be copied over (as well as a few new ones added at the
end). So (for simplicity and without actual loss of generality) say you have L sublists of I items each: the first I items
are copied back and forth L-1 times, the second I items L-2 times, and so on; total number of copies is I times the
sum of x for x from 1 to L excluded, i.e., I * (L**2)/2.

The list comprehension just generates one list, once, and copies each item over (from its original place of residence
to the result list) also exactly once.

Section 21.4: Dictionary Comprehensions


A dictionary comprehension is similar to a list comprehension except that it produces a dictionary object instead of
a list.

A basic example:

Python 2.x Version ≥ 2.7

{x: x * x for x in (1, 2, 3, 4)}


# Out: {1: 1, 2: 4, 3: 9, 4: 16}

which is just another way of writing:

dict((x, x * x) for x in (1, 2, 3, 4))


# Out: {1: 1, 2: 4, 3: 9, 4: 16}

As with a list comprehension, we can use a conditional statement inside the dict comprehension to produce only
the dict elements meeting some criterion.

Python 2.x Version ≥ 2.7

{name: len(name) for name in ('Stack', 'Overflow', 'Exchange') if len(name) > 6}


# Out: {'Exchange': 8, 'Overflow': 8}

Or, rewritten using a generator expression.

GoalKicker.com – Python® Notes for Professionals 135


dict((name, len(name)) for name in ('Stack', 'Overflow', 'Exchange') if len(name) > 6)
# Out: {'Exchange': 8, 'Overflow': 8}

Starting with a dictionary and using dictionary comprehension as a key-value pair filter

Python 2.x Version ≥ 2.7

initial_dict = {'x': 1, 'y': 2}


{key: value for key, value in initial_dict.items() if key == 'x'}
# Out: {'x': 1}

Switching key and value of dictionary (invert dictionary)

If you have a dict containing simple hashable values (duplicate values may have unexpected results):

my_dict = {1: 'a', 2: 'b', 3: 'c'}

and you wanted to swap the keys and values you can take several approaches depending on your coding style:

swapped = {v: k for k, v in my_dict.items()}


swapped = dict((v, k) for k, v in my_dict.iteritems())
swapped = dict(zip(my_dict.values(), my_dict))
swapped = dict(zip(my_dict.values(), my_dict.keys()))
swapped = dict(map(reversed, my_dict.items()))

print(swapped)
# Out: {a: 1, b: 2, c: 3}

Python 2.x Version ≥ 2.3

If your dictionary is large, consider importing itertools and utilize izip or imap.

Merging Dictionaries

Combine dictionaries and optionally override old values with a nested dictionary comprehension.

dict1 = {'w': 1, 'x': 1}


dict2 = {'x': 2, 'y': 2, 'z': 2}

{k: v for d in [dict1, dict2] for k, v in d.items()}


# Out: {'w': 1, 'x': 2, 'y': 2, 'z': 2}

However, dictionary unpacking (PEP 448) may be a preferred.

Python 3.x Version ≥ 3.5

{**dict1, **dict2}
# Out: {'w': 1, 'x': 2, 'y': 2, 'z': 2}

Note: dictionary comprehensions were added in Python 3.0 and backported to 2.7+, unlike list comprehensions,
which were added in 2.0. Versions < 2.7 can use generator expressions and the dict() builtin to simulate the
behavior of dictionary comprehensions.

Section 21.5: List Comprehensions with Nested Loops


List Comprehensions can use nested for loops. You can code any number of nested for loops within a list
comprehension, and each for loop may have an optional associated if test. When doing so, the order of the for

GoalKicker.com – Python® Notes for Professionals 136


constructs is the same order as when writing a series of nested for statements. The general structure of list
comprehensions looks like this:

[ expression for target1 in iterable1 [if condition1]


for target2 in iterable2 [if condition2]...
for targetN in iterableN [if conditionN] ]

For example, the following code flattening a list of lists using multiple for statements:

data = [[1, 2], [3, 4], [5, 6]]


output = []
for each_list in data:
for element in each_list:
output.append(element)
print(output)
# Out: [1, 2, 3, 4, 5, 6]

can be equivalently written as a list comprehension with multiple for constructs:

data = [[1, 2], [3, 4], [5, 6]]


output = [element for each_list in data for element in each_list]
print(output)
# Out: [1, 2, 3, 4, 5, 6]

Live Demo

In both the expanded form and the list comprehension, the outer loop (first for statement) comes first.

In addition to being more compact, the nested comprehension is also significantly faster.

In [1]: data = [[1,2],[3,4],[5,6]]


In [2]: def f():
...: output=[]
...: for each_list in data:
...: for element in each_list:
...: output.append(element)
...: return output
In [3]: timeit f()
1000000 loops, best of 3: 1.37 µs per loop
In [4]: timeit [inner for outer in data for inner in outer]
1000000 loops, best of 3: 632 ns per loop

The overhead for the function call above is about 140ns.

Inline ifs are nested similarly, and may occur in any position after the first for:

data = [[1], [2, 3], [4, 5]]


output = [element for each_list in data
if len(each_list) == 2
for element in each_list
if element != 5]
print(output)
# Out: [2, 3, 4]

Live Demo

For the sake of readability, however, you should consider using traditional for-loops. This is especially true when
nesting is more than 2 levels deep, and/or the logic of the comprehension is too complex. multiple nested loop list

GoalKicker.com – Python® Notes for Professionals 137


comprehension could be error prone or it gives unexpected result.

Section 21.6: Generator Expressions


Generator expressions are very similar to list comprehensions. The main difference is that it does not create a full
set of results at once; it creates a generator object which can then be iterated over.

For instance, see the difference in the following code:

# list comprehension
[x**2 for x in range(10)]
# Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Python 2.x Version ≥ 2.4

# generator comprehension
(x**2 for x in xrange(10))
# Output: <generator object <genexpr> at 0x11b4b7c80>

These are two very different objects:

the list comprehension returns a list object whereas the generator comprehension returns a generator.

generator objects cannot be indexed and makes use of the next function to get items in order.

Note: We use xrange since it too creates a generator object. If we would use range, a list would be created. Also,
xrange exists only in later version of python 2. In python 3, range just returns a generator. For more information,
see the Differences between range and xrange functions example.

Python 2.x Version ≥ 2.4

g = (x**2 for x in xrange(10))


print(g[0])

Traceback (most recent call last):


File "<stdin>", line 1, in <module>
TypeError: 'generator' object has no attribute '__getitem__'

g.next() # 0
g.next() # 1
g.next() # 4
...
g.next() # 81

g.next() # Throws StopIteration Exception

Traceback (most recent call last):


File "<stdin>", line 1, in <module>
StopIteration

Python 3.x Version ≥ 3.0

NOTE: The function g.next() should be substituted by next(g) and xrange with range since
Iterator.next() and xrange() do not exist in Python 3.

Although both of these can be iterated in a similar way:

GoalKicker.com – Python® Notes for Professionals 138


for i in [x**2 for x in range(10)]:
print(i)

"""
Out:
0
1
4
...
81
"""

Python 2.x Version ≥ 2.4

for i in (x**2 for x in xrange(10)):


print(i)

"""
Out:
0
1
4
.
.
.
81
"""

Use cases

Generator expressions are lazily evaluated, which means that they generate and return each value only when the
generator is iterated. This is often useful when iterating through large datasets, avoiding the need to create a
duplicate of the dataset in memory:

for square in (x**2 for x in range(1000000)):


#do something

Another common use case is to avoid iterating over an entire iterable if doing so is not necessary. In this example,
an item is retrieved from a remote API with each iteration of get_objects(). Thousands of objects may exist, must
be retrieved one-by-one, and we only need to know if an object matching a pattern exists. By using a generator
expression, when we encounter an object matching the pattern.

def get_objects():
"""Gets objects from an API one by one"""
while True:
yield get_next_item()

def object_matches_pattern(obj):
# perform potentially complex calculation
return matches_pattern

def right_item_exists():
items = (object_matched_pattern(each) for each in get_objects())
for item in items:
if item.is_the_right_one:

return True
return False

GoalKicker.com – Python® Notes for Professionals 139


Section 21.7: Set Comprehensions
Set comprehension is similar to list and dictionary comprehension, but it produces a set, which is an unordered
collection of unique elements.

Python 2.x Version ≥ 2.7

# A set containing every value in range(5):


{x for x in range(5)}
# Out: {0, 1, 2, 3, 4}

# A set of even numbers between 1 and 10:


{x for x in range(1, 11) if x % 2 == 0}
# Out: {2, 4, 6, 8, 10}

# Unique alphabetic characters in a string of text:


text = "When in the Course of human events it becomes necessary for one people..."
{ch.lower() for ch in text if ch.isalpha()}
# Out: set(['a', 'c', 'b', 'e', 'f', 'i', 'h', 'm', 'l', 'o',
# 'n', 'p', 's', 'r', 'u', 't', 'w', 'v', 'y'])

Live Demo

Keep in mind that sets are unordered. This means that the order of the results in the set may differ from the one
presented in the above examples.

Note: Set comprehension is available since python 2.7+, unlike list comprehensions, which were added in 2.0. In
Python 2.2 to Python 2.6, the set() function can be used with a generator expression to produce the same result:

Python 2.x Version ≥ 2.2

set(x for x in range(5))


# Out: {0, 1, 2, 3, 4}

Section 21.8: Refactoring filter and map to list


comprehensions
The filter or map functions should often be replaced by list comprehensions. Guido Van Rossum describes this
well in an open letter in 2005:

filter(P, S) is almost always written clearer as [x for x in S if P(x)], and this has the huge
advantage that the most common usages involve predicates that are comparisons, e.g. x==42, and
defining a lambda for that just requires much more effort for the reader (plus the lambda is slower than
the list comprehension). Even more so for map(F, S) which becomes [F(x) for x in S]. Of course, in
many cases you'd be able to use generator expressions instead.

The following lines of code are considered "not pythonic" and will raise errors in many python linters.

filter(lambda x: x % 2 == 0, range(10)) # even numbers < 10


map(lambda x: 2*x, range(10)) # multiply each number by two
reduce(lambda x,y: x+y, range(10)) # sum of all elements in list

Taking what we have learned from the previous quote, we can break down these filter and map expressions into
their equivalent list comprehensions; also removing the lambda functions from each - making the code more
readable in the process.

GoalKicker.com – Python® Notes for Professionals 140


# Filter:
# P(x) = x % 2 == 0
# S = range(10)
[x for x in range(10) if x % 2 == 0]

# Map
# F(x) = 2*x
# S = range(10)
[2*x for x in range(10)]

Readability becomes even more apparent when dealing with chaining functions. Where due to readability, the
results of one map or filter function should be passed as a result to the next; with simple cases, these can be
replaced with a single list comprehension. Further, we can easily tell from the list comprehension what the outcome
of our process is, where there is more cognitive load when reasoning about the chained Map & Filter process.

# Map & Filter


filtered = filter(lambda x: x % 2 == 0, range(10))
results = map(lambda x: 2*x, filtered)

# List comprehension
results = [2*x for x in range(10) if x % 2 == 0]

Refactoring - Quick Reference

Map

map(F, S) == [F(x) for x in S]

Filter

filter(P, S) == [x for x in S if P(x)]

where F and P are functions which respectively transform input values and return a bool

Section 21.9: Comprehensions involving tuples


The for clause of a list comprehension can specify more than one variable:

[x + y for x, y in [(1, 2), (3, 4), (5, 6)]]


# Out: [3, 7, 11]

[x + y for x, y in zip([1, 3, 5], [2, 4, 6])]


# Out: [3, 7, 11]

This is just like regular for loops:

for x, y in [(1,2), (3,4), (5,6)]:


print(x+y)
# 3
# 7
# 11

Note however, if the expression that begins the comprehension is a tuple then it must be parenthesized:

[x, y for x, y in [(1, 2), (3, 4), (5, 6)]]

GoalKicker.com – Python® Notes for Professionals 141


# SyntaxError: invalid syntax

[(x, y) for x, y in [(1, 2), (3, 4), (5, 6)]]


# Out: [(1, 2), (3, 4), (5, 6)]

Section 21.10: Counting Occurrences Using Comprehension


When we want to count the number of items in an iterable, that meet some condition, we can use comprehension
to produce an idiomatic syntax:

# Count the numbers in `range(1000)` that are even and contain the digit `9`:
print (sum(
1 for x in range(1000)
if x % 2 == 0 and
'9' in str(x)
))
# Out: 95

The basic concept can be summarized as:

1. Iterate over the elements in range(1000).


2. Concatenate all the needed if conditions.
3. Use 1 as expression to return a 1 for each item that meets the conditions.
4. Sum up all the 1s to determine number of items that meet the conditions.

Note: Here we are not collecting the 1s in a list (note the absence of square brackets), but we are passing the ones
directly to the sum function that is summing them up. This is called a generator expression, which is similar to a
Comprehension.

Section 21.11: Changing Types in a List


Quantitative data is often read in as strings that must be converted to numeric types before processing. The types
of all list items can be converted with either a List Comprehension or the map() function.

# Convert a list of strings to integers.


items = ["1","2","3","4"]
[int(item) for item in items]
# Out: [1, 2, 3, 4]

# Convert a list of strings to float.


items = ["1","2","3","4"]
map(float, items)
# Out:[1.0, 2.0, 3.0, 4.0]

Section 21.12: Nested List Comprehensions


Nested list comprehensions, unlike list comprehensions with nested loops, are List comprehensions within a list
comprehension. The initial expression can be any arbitrary expression, including another list comprehension.

#List Comprehension with nested loop


[x + y for x in [1, 2, 3] for y in [3, 4, 5]]
#Out: [4, 5, 6, 5, 6, 7, 6, 7, 8]

#Nested List Comprehension


[[x + y for x in [1, 2, 3]] for y in [3, 4, 5]]
#Out: [[4, 5, 6], [5, 6, 7], [6, 7, 8]]

GoalKicker.com – Python® Notes for Professionals 142


The Nested example is equivalent to

l = []
for y in [3, 4, 5]:
temp = []
for x in [1, 2, 3]:
temp.append(x + y)
l.append(temp)

One example where a nested comprehension can be used it to transpose a matrix.

matrix = [[1,2,3],
[4,5,6],
[7,8,9]]

[[row[i] for row in matrix] for i in range(len(matrix))]


# [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Like nested for loops, there is no limit to how deep comprehensions can be nested.

[[[i + j + k for k in 'cd'] for j in 'ab'] for i in '12']


# Out: [[['1ac', '1ad'], ['1bc', '1bd']], [['2ac', '2ad'], ['2bc', '2bd']]]

Section 21.13: Iterate two or more list simultaneously within


list comprehension
For iterating more than two lists simultaneously within list comprehension, one may use zip() as:

>>> list_1 = [1, 2, 3 , 4]


>>> list_2 = ['a', 'b', 'c', 'd']
>>> list_3 = ['6', '7', '8', '9']

# Two lists
>>> [(i, j) for i, j in zip(list_1, list_2)]
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]

# Three lists
>>> [(i, j, k) for i, j, k in zip(list_1, list_2, list_3)]
[(1, 'a', '6'), (2, 'b', '7'), (3, 'c', '8'), (4, 'd', '9')]

# so on ...

GoalKicker.com – Python® Notes for Professionals 143

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy