Show Menu
Cheatography

Pythons Tips Cheat Sheet (DRAFT) by

Some Python tips ....

This is a draft cheat sheet. It is a work in progress and is not finished yet.

List Compre­hension

list = [x for x in list_variable/range(n) if/else statement] 

from collections import Counter
counter = counter(AnotherList)
counter.most_commons(n)[0][0] # get the n most occurated value in the list
max(List) # get the max value
set(HugeList) # remove duplicate values

Map Function

# Useful instead of for loop, returns a map object -> need to transform as a list
map(function, iterable, [iterable 2, iterable 3, ...]) # General expression
mapped_numbers = list(map(lambda x: x * 2 + 3, numbers)) # Exemple

Dictio­nnaries

dict1.update(dict2) # merge the 2 dictionnaries together
dict3 = {**dict1, **dict2} # Merge the 2 dictionnaries into a 3rd one
dict3 = dict1 | dict2 # also work
dict3 = dict(dict1.items() | dict2.items()) # merge the 2 dictionnaries together randomly
 

Lambda expression

Join Function

variable = " ".join(YourList)