kuniga.me > Docs > Python Cheatsheet
Syntax for common tasks I run into often. Assumes Python 3.
- Map over list
- Filter list
- Sort list
- Map over dictionary
- Sort dictionary
- Map over set
- Object Oriented
- Class Methods
- Read file
- Other Data Structures
Map over list
NOTE: filter and map can be combined into one.
Using default sorting:
Sorting using key function. For example, sort list of pairs by the second element:
Sorting by custom comparator:
Map over dictionary
Dictionaries are unordered sets, so you likely want to work with a list after sorting.
Map over set
Check if object is instance of a class:
Methods where the bound variable is an instance to the class.
See also: Class Methods in Revisiting Python: Object Oriented Programming
Lightweight syntax for creating classes / records.
Other Data Structures
Queue class is an advanced implementation that can be used for example in multi-thread applications. We can still use it as a plain queue data structure.