Click here to Skip to main content
15,890,186 members
Articles / Programming Languages / Python2.7
Tip/Trick

Python Code Optimizations : Part 1

Rate me:
Please Sign up or sign in to vote.
4.93/5 (12 votes)
14 Oct 2014CPOL1 min read 16.2K   12   2
Follow the tips below to write more beautiful and idiomatic Python code!

Introduction

Listed below are some very common day to day usage scenarios, and a way to do them pythonically!

Using the Code

1. Looping over a Range of Numbers

C++
for i in [0,1,2,3,4,5]:
    print i**2
Better way (looks better):
C++
for i in range(6):
    print i**2

What Happens in this Loop?

range produces a list in memory and then the for loop loops over the list.
Both create a list of 6 integers in memory and then iterate over each number, raise it to power 2 and then print. Thus, both the loops do exactly the same thing in exactly the same way!

Pythonic way: Use xrange()
C++
#Python 2.x
for i in xrange(6):
    print i**2

#Python 3.x
for i in range(6):
    print i**2

What is xrange?

  • xrange is a sequence object that evaluates lazily.
  • xrange creates an iterator over the range(list) and yields one number at a time, thus consuming less amount of memory than the methods above.

2. Looping Over a Collection

colours = ['red', 'green', 'blue', 'yellow']

for i in range(len(colours)):
    print colours[i]
Pythonic way
for colour in colours:
    print colour

3. Looping Over a Collection and its Indices

for i in range(len(colours)):
    print i, '-->', colours[i]
Pythonic way: use enumerate()
for i, colour in enumerate(colours):
    print i, '-->', colour

4. Looping Backwards

for i in range(len(colours), -1, -1, -1):
    print colours[i]
Pythonic way: Use reversed()
for colour in reversed(colours):
    print colour

5. Loooping in Sorted Order

Pythonic way: Use sorted()
for colour in sorted(colours):
    print colour

Looping Backwards in Sorted Order

Just add reverse=True to the sorted function arguments list.

Pythonic Way
for colour in sorted(colours, reverse=True):
    print colour

6. Looping Over Two Collections

names = ['a', 'b', 'c']
colours = ['red', 'green', 'blue', 'yellow']

n = min(len(colours), len(names))

for i in range(n):
    print names[i], '-->', colours[i]
Better Way
for name, colour in zip(names, colours):
    print name, '-->', colour    

zip creates a third list in memory which consists of tuples, each of which is its own separate object with pointers back to the original. In other words, it takes far more memory than the original two lists combined.
Most importantly "It doesn't scale!".

Pythonic Way: use izip()
from itertools import izip
for name, colour in izip(names, colours):
    print name, '-->', colour

For smaller lists, zip is faster, but if you have lists with millions of records, then better use izip, as izip only advances the underlying iterators when needed.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Mindfire Solutions
India India
Writing python code @ Mindfire Solutions, Noida.

Comments and Discussions

 
Questionnice Pin
raildude2-Dec-14 5:52
professionalraildude2-Dec-14 5:52 
AnswerRe: nice Pin
Dhruv Singh2-Dec-14 6:20
professionalDhruv Singh2-Dec-14 6:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.