Insights from Our Experts

Blog image

Python optimization techniques

Author Image

Joshwin Vadakkadom,Software Engineer

Coding Standard is an important criteria for the career of a Developer. In this, Code Optimisation is a must. Here are some techniques for the developers to optimize the code of Python Programming Language.


Lambda Function

We can simply define a lambda function as a function without a name  i.e,  an anonymous function. It can be used with the following functions as well:  filter(), map() and reduce(). 

Simple Example :

>>> f = lambda x,y: x/y
>>> f(1,1)
Output: 1



map(function, list)

Here the first parameter of the map() is a function and other one is a list.


>>> k = [10,11,12,13]
>>> p = map(lambda x:x*10, k)
>>> print p
Output :- [100, 110, 120, 130]



filter(function, list)

Here also the first parameter of the filter() is a function and the other one is a list.

>>>p =[1,2,3,4,5,6,7,8,9,10]
>>> ti = filter(lambda x: x % 2,p)
>>> print ti
Output = [1, 3, 5, 7, 9]

Here it filters the set of odd numbers from the list



Similar to previous functions, the first parameter of the reduce() is a function and the other one is a list.

>>> p=range(1,5)
>>> k = reduce(lambda x,y:x+y, p)
>>> print k
Output : 10

Flow of reducing function is given below:
p = 1,2,3,4
step 1 = 1+2=3
step 2= step 1 + 3 = 6
step 3= step 2 + 4 = 10


List Comprehension
List comprehension can be used in many cases as a substitute for lengthy codes. It is basically used for creating Lists.

Here is an example:- 

>>> q = [ ] 
>>> for i in range(5,10):
...   for j in range(i*2,20):
...     q.append(j)
>>> print q

Output will be : q=[ 10,11,12,13,14,15,16,17,18,19,20,12,13,14,15,16,17,18,19,14,15,16,17,18,19,16,17,18,19,

This code takes almost 6 lines, we can standardise the code and reduce the number of lines.

>>> a= [j for i in range(5,10) for j in range(i*2,100)]

List Comprehension can be used in many other cases like substituting map(), filter() etc.


Dictionary Comprehension

Dictionary comprehension is used for optimizing the code in case of creating a dictionary.


>>> d = {k: k*3 for k in range(1,5)}
>>> print d
Output : {1: 3, 2: 6, 3: 9, 4: 12}

Using Imports Properly

Consider a scenario in which a particular package needs to be imported from a particular module. If we specify the package and module then it is optimised 

For example

Normal way : from country import *

Correct way : from country.india import states

We can specifically import the required thing and can avoid importing the entire package. 


Using  Lazy Generators  

Consider the code given below:

>>> n=sum(range(100))
>>> print n
Output: 4950

>>> n=sum(xrange(100))
>>> print n
Output: 4950

Here in the first section of the code by using range, we built 100 elements in the memory and found its sum. This is a waste of memory. So in order to optimize the  code, xrange creates a lazy generator, when we use the xrange it generate each number in which sum will consume to accumulate the sum. So memory will not be wasted and hence code will be optimized.


Use ‘try except’ Instead of ‘if else’

if else
if a==b:
else :

try except
except a==b:

In the second case, the interpreter executes the sub() first and throws an exception in the exceptional case but in the first case the interpreter has to search each time the ‘if statement’ is executed, because the name could refer to something different since the last time the statement was executed. It is better to use the try except for a better optimization.



It is used to get an analysis of a program which is executed, i.e how often or how long it gets used. We can use several profiling modules in a profile and the function execution gets easy. Suppose your main function say sample() takes no argument and you can execute this under the control of profile module. 

import profile‘sample()’)

It will print a table of function calls corresponding to the execution times .
Speed of execution will be higher from the normal way.    

I Need

Help for