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

Python Code Optimizations: Part 3

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
25 Nov 2015CPOL1 min read 18.6K   13   1
Follow the tips below to write more beautiful and idiomatic Python code!

Introduction

Part I and Part II of this tip can be viewed from the respective links.

Using the Code

1. Looping over Dictionary Keys and Values

for k in d:
    print k, '-->', d[k]

In the above example, we loop over the keys and then lookup the value. It is not very fast because it will have to rehash every key and do a lookup on it.

Instead, do this to get both keys and values.

for k,v in d.items():
    print k, '-->', v

Here, we're using tuple unpacking. So when you need both keys and values, loop over them directly and hence no lookups are required. Also, items() makes a list in the memory, which is not required.

Pythonic Way: use iteritems()

for k,v in d.iteritems():
    print k, '-->', v

2. Construct a Dictionary from Two Lists

Use zip().

names = ['steve','bill','jeff']
org = ['apple', 'microsoft', 'amazon']

d = dict(zip(names, org))

Pythonic Way: use izip()

from itertools import izip
d = dict(izip(names, org))

Many would think that izip will make a tuple at each iteration, but what it does is after the dictionary has consumed the tuple, we loop back around to make the next tuple, we re-use the previous one, so it can build the dictionary using the same tuple over and over again.

3. Counting with Dictionaries

colours = ['blue','red','green','red','red', 'green']
d = {}
for colour in colours:
    if colour not in d:
        d[colour] = 0
    d[colour] += 1

Better Way: use get() method

d = {}
for colour in colours:
    d[colour] = d.get(colour, 0) + 1

Pythonic Way: use defaultdict or Counter

from collections import defaultdict
d = defaultdict(int)
for colour in colours:
    d[colour] += 1

Although in the above code, d will be an object of defaultdict, which will have to be converted to a dict, if you intend to use it as one!

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

 
GeneralMy vote of 5 Pin
raildude4-Dec-14 7:50
professionalraildude4-Dec-14 7:50 

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.