Cool Python tricks and tips
Here are some cool tricks to write better python code:
List comprehensions:
Instead of building a list with a loop:
|
1 2 3 4 |
b = [] for x in a: b.append(10 * x) foo(b) |
We can often build it much more concisely with a list comprehension:
|
1 |
b = [10 * x for x in a] |
Enumerate
We can use enumerate to do a for loop:
|
1 2 3 4 |
i = 0 for item in iterable: print i, item i += 1 |
like this:
|
1 2 |
for i, item in enumerate(iterable): print i, item |
Enumerate can also take a second argument. Here is an example:
|
1 2 3 4 5 |
>>> list(enumerate('abc')) [(0, 'a'), (1, 'b'), (2, 'c')] >>> list(enumerate('abc', 1)) [(1, 'a'), (2, 'b'), (3, 'c')] |
Dict/Set comprehensions
dict/set comprehensions are simple to use and just as effective:
|
1 2 3 4 |
my_dict = {i: i * i for i in xrange(100)} my_set = {i * 15 for i in xrange(100)} # There is only a difference of ':' in both |
Simple Server
In can we can simply start a web server like this:
|
1 2 3 4 5 |
# Python2 python -m SimpleHTTPServer 8080 # Python 3 python3 -m http.server 8080 |
After you hit enter, you should see the following message:
Serving HTTP on 0.0.0.0 port 8080 …
Open your favorite browser and put in any of the following addresses:
http://your_ip_address:8080
http://127.0.0.1:8080
Evaluating Python expressions
The following two do the same thing:
|
1 2 |
import ast my_list = ast.literal_eval(expr) |
|
1 2 |
expr = "[1, 2, 3]" my_list = eval(expr) |
Object introspection
We can inspect objects in Python by using dir():
|
1 2 3 4 5 6 |
>>> foo = [1, 2, 3, 4] >>> dir(foo) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', ... , 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] |
Simplify if constructs
If we have to check for several values we can do:
|
1 |
if n in [1,4,5,6]: |
instead of:
|
1 |
if n==1 or n==4 or n==5 or n==6: |
Reversing a list/string
To reverse a list:
|
1 2 3 4 5 6 7 8 9 10 |
>>> a = [1,2,3,4] >>> a[::-1] [4, 3, 2, 1] # This creates a new reversed list. # If you want to reverse a list in place you can do: a.reverse() |
and the same can be applied to a string as well:
|
1 2 3 |
>>> foo = "yasoob" >>> foo[::-1] 'boosay' |
Pretty print
You can print dicts and lists in a beautiful way by doing:
|
1 2 |
from pprint import pprint pprint(my_dict) |
This is more effective on dicts. Moreover, if you want to pretty print json quickly from a file then you can simply do:
|
1 |
cat file.json | python -m json.tools |
Ternary Operators
Ternary operators are shortcut for an if-else statement, and are also known as a conditional operators.
|
1 2 3 |
[on_true] if [expression] else [on_false] x, y = 50, 25 small = x if x < y else y |
Fun tricks with zip
zipTransposing a matrix:
>>> l = [[1, 2, 3], [4, 5, 6]]>>> zip(*l)[(1, 4), (2, 5), (3, 6)]
Dividing a list into groups of :n
>>> l = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8]>>> zip(*[iter(l)] * 3)[(3, 1, 4), (1, 5, 9), (2, 6, 5), (3, 5, 8)]
Another example to use zip
Write a Python code to print
|
1 2 |
list1 = ['a', 'b', 'c', 'd'] list2 = ['p', 'q', 'r', 's'] |
ap
bq
cr
ds
|
1 2 |
for x, y in zip(list1,list2): print x, y |
- …
- a p
- b q
- c r
- d s
Create a String from a list:
|
1 |
a = ["Code", "mentor", "Python", "Developer"] |
Create a single string from all the elements in list above.
|
1 |
print " ".join(a) |
Swap two numbers with one line of code.
-
>>> a=7
-
>>> b=5
-
>>> b, a =a, b
-
>>> a
-
5
-
>>> b
-
7
repeat strings
print “codecodecodecode mentormentormentormentorm
- >>> print “code”*4+‘ ‘+“mentor”*5
codecodecodecode mentormentormentormentorm
Flatten a nested list
Convert it to a single list without using any loops.
- a = [[1, 2], [3, 4], [5, 6]]
Output:- [1, 2, 3, 4, 5, 6]
- >>> import itertools
- >>> list(itertools.chain.from_iterable(a))
- [1, 2, 3, 4, 5, 6]
Use Map
Take a string input.
For example “1 2 3 4” and return [1, 2, 3, 4]
Remember list being returned has integers in it. Don’t use more than one line of code.
- >>> result = map(lambda x:int(x) ,raw_input().split())
- 1 2 3 4
- >>> result
- [1, 2, 3, 4]
startswith parameters
Instead of:
if s.startswith('http://') or s.startswith('https://'):
You can use:
if s.startswith(('http://', 'https://')):
NOTE: And the same with endswith and isinstance(1, (int, float))
Be careful with mutable default arguments
|
1 2 3 4 5 6 7 8 9 10 |
>>> def foo(x=[]): ... x.append(1) ... print x ... >>> foo() [1] >>> foo() [1, 1] >>> foo() [1, 1, 1] |
Instead, you should use a sentinel value denoting “not given” and replace with the mutable you’d like as default:
|
1 2 3 4 5 6 7 8 |
>>> def foo(x=None): ... x = x or [] ... x.append(1) ... print x >>> foo() [1] >>> foo() [1] |
The Awesome Module Itertools
Along with the collections library python also has a library called itertools which has really cool efficient solutions to problems. One is finding all combinations. This will tell us all the different ways the teams can play each other.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
>>> from itertools import combinations >>> teams = ["Packers", "49ers", "Ravens", "Patriots"] >>> for game in combinations(teams, 2): ... print game >>> ('Packers', '49ers') >>> ('Packers', 'Ravens') >>> ('Packers', 'Patriots') >>> ('49ers', 'Ravens') >>> ('49ers', 'Patriots') >>> ('Ravens', 'Patriots') |
Named tuples:
|
1 2 3 4 5 |
>>> Point = collections.namedtuple('Point', ['x', 'y']) >>> p = Point(x=1.0, y=2.0) >>> p Point(x=1.0, y=2.0) |
Now you can index by keyword, much nicer than offset into tuple by number (less readable)
|
1 2 3 4 5 |
>>> p.x 1.0 >>> p.y 2.0 |
Elegantly used when looping through a csv:
|
1 2 3 4 5 6 7 8 |
with open('stock.csv') as f: f_csv = csv.reader(f) headings = next(f_csv) Row = namedtuple('Row', headings) for r in f_csv: row = Row(*r) # note the star extraction # ... process row ... |
We can use the unpacking star feature to throw away useless fields:
|
1 2 3 4 5 6 7 8 9 |
line = 'nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false' >>> uname, *fields, homedir, sh = line.split(':') >>> uname 'nobody' >>> homedir '/var/empty' >>> sh '/usr/bin/false' |
Use the defaultdict:
|
1 2 3 4 5 |
from collections import defaultdict rows_by_date = defaultdict(list) for row in rows: rows_by_date[row['date']].append(row)", |
So we don’t need to init the list each time which leads to needless code:
|
1 2 3 |
if row['date'] not in rows_by_date: rows_by_date[row['date']] = [] |
You can use OrderedDict to leave the order of inserted keys:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
>>> import collections >>> d = collections.OrderedDict() >>> d['a'] = 'A' >>> d['b'] = 'B' >>> d['c'] = 'C' >>> d['d'] = 'D' >>> d['e'] = 'E' >>> for k, v in d.items(): ... print k, v ... a A b B c C d D e E |
Counter:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
from collections import Counter words = [ 'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes', 'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the', 'eyes', ""don't"", 'look', 'around', 'the', 'eyes', 'look', 'into', 'my', 'eyes', ""you're"", 'under' ] word_counts = Counter(words) top_three = word_counts.most_common(3) print(top_three) # Outputs [('eyes', 8), ('the', 5), ('look', 4)]", |
Sorted by specified column
sorted() accepts a key arg which you can use to sort on something else
For example:
|
1 2 |
>>> sorted(names, key=lambda name: name.split()[-1].lower()) ['Ned Batchelder', 'David Beazley', 'Raymond Hettinger', 'Brian Jones'] |
Create XMl from dict
Creating XML tags manually is usually a bad idea, I bookmarked this simple dict_to_xml helper:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from xml.etree.ElementTree import Element def dict_to_xml(tag, d): ''' Turn a simple dict of key/value pairs into XML ''' elem = Element(tag) for key, val in d.items(): child = Element(key) child.text = str(val) elem.append(child) return elem" |
One line code to check if a file exists in a directory
|
1 2 3 4 |
import os files = os.listdir('dirname') if any(name.endswith('.py') for name in files): |
Use set to get the common items in lists
Use set operations to match common items in lists
|
1 2 3 4 5 |
>>> a = [1, 2, 3, 'a'] >>> b = ['a', 'b', 'c', 3, 4, 5] >>> set(a).intersection(b) {3, 'a'} |
I hope these tricks are useful to you. Please leave a comment to share other python tricks.
https://gist.github.com/douglasmiranda/3262157
https://www.quora.com/What-are-some-cool-Python-tricks
http://bobbelderbos.com/2016/06/python-tips/











