Examples, FAQs (Frequently Asked Questions), notebooks and other little bits of code that we hope to be able to copy paste without understanding them. Sometimes it’s true.

## Classic Construction

### Calculation of a Sum

Calculation of a sum always involves a loop because the Python language can add only two numbers. The schema is always the same: initialization and loop.

1 2 3 4 5 6 7 |
li = [0, 434, 43, 6456] s = 0 # initialization for l in li: # loop s += l # addition print(s) |

**Output:**

1 2 3 |
6933 |

This code is equivalent to the sum function. In this case where the sum integrates the result of a function (in the mathematical sense) and not the elements of a list, it would be necessary to write:

1 2 3 4 5 6 7 8 9 10 11 |
def func(x): return x li = [0, 434, 43, 6456] s = 0 for l in li: s += func(l) print(s) |

**Output:**

1 2 3 |
6933 |

And these two lines could be summed up in one thanks to one of these instructions:

1 2 3 4 5 6 7 8 9 10 11 |
def func(x): return x li = [0, 434, 43, 6456] s1 = sum([func(l) for l in li]) s2 = sum(func(l) for l in li) s3 = sum(map(func, li)) print(s1, s2, s3) |

**Output:**

1 2 3 |
6933 6933 6933 |

The advantage of the last two instructions is that they avoid the creation of an intermediate list, it is a point to take into account if the list on which operates the sum is voluminous.

### Calculating the sum of the first ten integers squared

This simple calculation can be written in different ways.

1 2 3 4 5 |
s = 0 for i in range(1,11): s += i**2 |

In short:

1 2 3 |
s = sum ( [ i**2 for i in range(1,11) ] ) |

### Count

Here we want to count the number of occurrences of each element of an array. For example, one could know by this means the popularity of a word in a political speech or the extent of the vocabulary used. The following example counts the words in a word list.

1 2 3 4 5 6 7 8 9 10 |
li = ["one", "two", "one", "three"] d = {} for l in li: if l not in d: d[l] = 1 else: d[l] += 1 print(d) # {'one': 2, 'two': 1, 'three': 1} |

**Output:**

1 2 3 |
{'one': 2, 'two': 1, 'three': 1} |

The most appropriate structure here is a dictionary since one tries to associate a value with an element of a list that can be of any type. If the list contains editable elements like a list, these should be converted into an immutable type such as a string. The following example illustrates this case by counting the occurrences of the rows of a matrix.

1 2 3 4 5 6 7 8 9 10 11 |
mat = [[1, 1, 1], [2, 2, 2], [1, 1, 1]] d = {} for l in mat: k = str(l) # k = tuple (l) where possible if k not in d: d[k] = 1 else: d[k] += 1 print(d) # {'[1, 1, 1]': 2, '[2, 2, 2]': 1} |

**Output:**

1 2 3 |
{'[1, 1, 1]': 2, '[2, 2, 2]': 1} |

One can also want not to count the number of occurrences but to memorize the positions of the elements all identical. One must use a dictionary of lists:

1 2 3 4 5 6 7 8 9 10 |
li = ["one", "two", "one", "three"] d = {} for i, v in enumerate(li): if v not in d: d[v] = [i] else: d[v].append(i) print(d) # {'one': [0, 2], 'two': [1], 'three': [3]} |

**Output:**

1 2 3 |
{'one': [0, 2], 'two': [1], 'three': [3]} |

If you just have to count, the simplest writing is:

1 2 3 4 5 6 7 |
r = {} li = ["one", "two", "one", "three"] for x in li: r[x] = r.get(x, 0) + 1 print(r) # {'one': 2, 'two': 1, 'three': 1} |

**Output:**

1 2 3 |
{'one': 2, 'two': 1, 'three': 1} |

### Converting a vector into a matrix

In a language like C ++, it often happens that a matrix is not represented by a list of lists but by a single list because this representation is more efficient. It is therefore necessary to convert an index into two row and column indices. Of course, the number of columns on each line must be constant. The first program converts a list of lists into a single list.

1 2 3 4 5 6 |
ncol = 2 vect = [0, 1, 2, 3, 4, 5] mat = [vect[i * ncol: (i + 1) * ncol] for i in range(0, len(vect) // ncol)] print(mat) #[[0, 1], [2, 3], [4, 5]] |

**Output:**

1 2 3 |
[[0, 1], [2, 3], [4, 5]] |

Converting a string to a datetime

This is the kind of function that we do not use often but it is hard to find when we need it. You have to use the strftime function.

1 2 3 4 |
import datetime dt = datetime.datetime.strptime ("16/01/2014", "%d/%m/%Y") |

### Converting a string into a matrix

The next few lines allow you to break a string into a matrix. Each row and column are separated by different separators. This process often occurs when information is retrieved from a text file itself from a spreadsheet.

1 2 3 4 5 6 7 8 |
s = "case11;case12;case13|case21;case22;case23" # matrix decomposition ligne = s.split("|") # lines mat = [l.split(";") for l in ligne] # columns print(mat) #[['case11', 'case12', 'case13'], ['case21', 'case22', 'case23']] |

**Output:**

1 2 3 |
[['case11', 'case12', 'case13'], ['case21', 'case22', 'case23']] |

As this operation is very common when working with the data, it is no longer implements itself. It is preferred to use a module as pandas that is more robust and considers more cases. To write, use the to_csv method, to read, the function read_csv. You can also directly save in Excel read_excel format and write in the same format to_excel.

### Converting a matrix to a string

1 2 3 4 5 6 7 |
mat = [['case11', 'case12', 'case13'], ['case21', 'case22', 'case23']] line = [";".join(l) for l in mat] # cols s = "|".join(line) # lines print(s) #case11;case12;case13|case21;case22;case23 |

**Output:**

1 2 3 |
case11;case12;case13|case21;case22;case23 |

### Converting a matrix into a vector

In a language like C ++, it often happens that a matrix is not represented by a list of lists but by a single list because this representation is more efficient. It is therefore necessary to convert an index into two row and column indices. Of course, the number of columns on each line must be constant. The first program converts a list of lists into a single list.

1 2 3 4 5 6 7 |
mat = [[0, 1, 2], [3, 4, 5]] lin = [i * len(mat[i]) + j for i in range(0, len(mat)) for j in range(0, len(mat[i]))] print(lin) |

**Output:**

1 2 3 |
[0, 1, 2, 3, 4, 5] |

**You may also like: Python Tutorial with Exercises**

You can also use functions such as reduce.

1 2 3 4 5 6 |
from functools import reduce mat = [[0, 1, 2], [3, 4, 5]] lin = reduce(lambda x, y: x + y, mat) print(lin) |

**Output:**

1 2 3 |
[0, 1, 2, 3, 4, 5] |

### Function as parameter

A function can also receive another function as parameter. The following example includes the function calculuevalue which takes as parameters l and f. This function calculates for all the values x in the list l the value f (x). function_carre or function_cube are passed in parameters to the function calculuevalue which executes them.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
def func_square(x): return x * x def func_cube(x): return x * x * x def calc_n_val(l, f): res = [f(i) for i in l] return res l = [0, 1, 2, 3] print(l) # [0, 1, 2, 3] l1 = calc_n_val(l, func_square) print(l1) # [0, 1, 4, 9] l2 = calc_n_val(l, func_cube) print(l2) # [0, 1, 8, 27] |

**Output:**

1 2 3 4 5 |
[0, 1, 2, 3] [0, 1, 4, 9] [0, 1, 8, 27] |

### Minimum with position

The min function returns the minimum of an array but not its position. The first reflex is then to recode the course of the list while maintaining the position of the minimum.

1 2 3 4 5 6 7 8 |
li = [0, 434, 43, 6436, 5] m = 0 for i in range(0, len(li)): if li[m] < li[i]: m = i print(m) |

**Output:**

1 2 3 |
3 |

But there is a trick to get the position without having to reprogram it.

1 2 3 4 5 6 |
li = [0, 434, 43, 6436, 5] k = [(v, i) for i, v in enumerate(li)] m = min(k) print(m) |

**Output:**

1 2 3 |
(0, 0) |

The min function chooses the minimum element of an array whose elements are couples (element of the first array, its position). The minimum is chosen by comparing the elements, and the position will disperse the equel.

### Search with index

When looking for an element in a table, one looks for its position more often than the fact that the array contains this element.

1 2 3 4 5 6 7 8 9 10 11 |
def research(li, c): for i, v in enumerate(li): if v == c: return i return -1 li = [45, 32, 43, 56] print(research(li, 43)) # 2 |

**Output:**

1 2 3 |
2 |

In python, there is a simple function that allows you to do that:

1 2 3 |
print(li.index(43)) # 2 |

When the element is not there, we often return the position -1 which can not be taken by any element:

1 2 3 4 |
if c in li: return li.index(c) else: return -1 |

Even if this piece of code runs twice over the table (once determining its presence, a second time for its position), this code is often faster than the first version and the probability of making a smaller error.

### two dimensional research

The two dimensional search is faster than a classical search but it assumes that it is done in a sorted set. The idea is to cut in half the search interval at each iteration. As the whole is sorted, by comparing the sought element to the central element, one can eliminate a part of the whole: the lower or upper half.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def reser_two_dim(li, c): a, b = 0, len(li) - 1 while a <= b: m = (a + b) // 2 if c == li[m]: return m elif c < li[m]: b = m - 1 else: a = m + 1 return -1 print(reser_two_dim([0, 2, 5, 7, 8], 7)) # 3 |

1 2 3 |
3 |

### Sort, keep the initial positions

Sorting is a frequent operation. We do not always have the time to program the most efficient sorting as a quicksort sort and a simpler sort is enough most of the time. The next sort is to find the smallest item and then swap its place with the first item in the table. We repeat the same procedure from the second position, then the third and so on until the end of the table.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
li = [5, 6, 4, 3, 8, 2] for i in range(0, len(li)): # search for the minimum between i and len (li) excluded pos = i for j in range(i + 1, len(li)): if li[j] < li[pos]: pos = j # exchange ech = li[pos] li[pos] = li[i] li[i] = ech print(li) |

**Output:**

1 2 3 |
[2, 3, 4, 5, 6, 8] |

The sorted function also sorts a list but uses a more efficient algorithm than this one (see Timsort). We sometimes have to reprogram a sort because we want to keep the position of the elements in the unsorted array. This happens when you want to sort an array and apply the same transformation to a second array. It is always best not to reprogram a sort (less error). Just apply the same idea as for the minindex function.

1 2 3 4 5 |
tab = ["zero", "one", "two"] # table to sort pos = sorted((t, i) for i, t in enumerate(tab)) # picture of couples print(pos) # [('one', 1), ('two', 2), ('zero', 0)] |

**Output:**

1 2 3 |
[('one', 1), ('two', 2), ('zero', 0)] |

If this writing is too succinct, it can be broken down into:

1 2 3 4 5 6 |
tab = ["zero", "one", "two"] tab_position = [(t, i) for i, t in enumerate(tab)] tab_position.sort() print(tab_position) # [('one', 1), ('two', 2), ('zero', 0)] |

**Output:**

1 2 3 |
[('one', 1), ('two', 2), ('zero', 0)] |

## Negative Constructions

### Avoid making the same call twice

In this function we calculate the variance of a series of observations.

1 2 3 4 5 6 7 8 9 10 |
def average(series): return sum(series) / len(series) def variance_to_avoid(series): s = 0 for obs in series : s += (obs-average(series))**2 return s / len(series) |

### Edit a dictionary by browsing it

You must avoid changing a container while traveling. When deleting an element from a dictionary, the structure of the dictionary is modified and affects the loop that traverses it. The loop always goes through the old dictionary structure, the one that existed at the beginning of the loop.

1 2 3 4 5 6 |
d = { k: k for k in range(10) } for k, v in d.items(): if k == 4 : del d[k] |

In Python, this produces the following error but other languages do not prevent (C ++) and this results in an error that occurs later in the code (such as an unexpected numeric value).

It is necessary to avoid that to store the elements which one wants to modify to remove them then.

1 2 3 4 5 6 7 8 9 |
d = { k:k for k in l } rem = [ ] for k,v in d.items(): if k == 4 : rem.append(k) for r in rem : del d[r] |

Even if Python allows this for lists, it is advisable to abstain as well as for any type of objects that contains others. It’s a habit that will serve you for most other languages.

[…] More examples: Python Examples […]