Python Tidbits

Optimized list count

for a speedy count of multiple items in a (big) list, use the Counter module.

from collections import Counter
l=[1,2,3,4,3,4,5,6,7,5,5,4,5,6,6,7,8,9]
c=Counter(l)

Out: Counter({1: 1, 2: 1, 3: 2, 4: 3, 5: 4, 6: 3, 7: 2, 8: 1, 9: 1})

For big lists, Counter performs much faster (100x in my test cases) than

{x: l.count(x) for x in l}

Product of lists

To create all possible combinations of the elements of two lists:

>>> from itertools import product
>>> animal = ['dog','cat']
>>> number = [5,45,8,9]
>>> color = ['yellow','blue','black']
>>> myProduct = product(animal, number, color)
>>> for p in myProduct:
...     print p
...     
('dog', 5, 'yellow')
('dog', 5, 'blue')
('dog', 5, 'black')
[...]
('cat', 9, 'blue')
('cat', 9, 'black')

The product result is an iterable, so once it’s looped, can’t access it anymore (needs rewind). It is better to convert to a list. From here.

String stuff

Remove non-ascii characters

def _removeNonAscii(s): return "".join(i for i in s if ord(i)<128)

Encode and decode

>>> 'abc'.decode('utf-8')  # str to unicode
u'abc'
>>> u'abc'.encode('utf-8') # unicode to str
'abc'

Join List of Strings With Separator

In[1]: ','.join(['a','b','c'])
Out[1]: 'a,b,c'

Reload modules

When changing a code inside a module, you need to reload the module if it was already imported:

In[1]: import foo
...
In[10]: reload(foo)

Global Variables

def set_globvar_to_one():
​    global globvar    # Needed to modify global copy of globvar
​    globvar = 1