Skip to content

Instantly share code, notes, and snippets.

@martinapugliese
Last active February 22, 2018 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinapugliese/765c66bc121f781f353d656ca883e4a7 to your computer and use it in GitHub Desktop.
Save martinapugliese/765c66bc121f781f353d656ca883e4a7 to your computer and use it in GitHub Desktop.

A reference on small Python stuffs

Various lil' things

See where the Python packages are installed

import site
site.getsitepackages()

Update a dictionary without using try except if key not there

d[k] = d.get(k, 0) + 1

Sort a dict by key or value

You get a sorted representation, the dict is not actually sorted

s = sorted(d.items(), key=operator.itemgetter(0))

This gives list of tuples sorted by key.

Find the max of list of tuples by second arg

max(day_numtweets, key=lambda x: x[1])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment