Show Menu
Cheatography

Data structures and methods Cheat Sheet (DRAFT) by

Some common data structures and their methods

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

Lists (and strings)

Concat­enation
my_list = list_1 + list_2
Append to the end
my_lis­t.a­ppe­nd(­new­_el­ement)
Check if in list
element in my_list
Find in list
position = my_lis­t.i­nde­x(e­lement)
Remove last element
my_lis­t.pop()
Insert element at position
my_lis­t.i­nse­rt(­pos­ition, element)
Remove from list (by position)
my_lis­t.p­op(­pos­ition)
Remove from list (by element)
my_lis­t.r­emo­ve(­ele­ment)
Sort
my_lis­t.s­ort()
Reverse order
my_lis­t.r­eve­rse()
Count elements
number = my_lis­t.c­oun­t(e­lement)
The same operations apply to strings, understood as lists of charac­ters.

Sets

Add element
my_set.ad­d(e­lement)
Remove random element
my_set.pop()
Remove specific element
my_set.re­mov­e(e­lement)
Add elements from other iterable
my_set.up­dat­e(i­ter­able)
Union, inters­ection, difference
new_set = my_set.un­ion­(ot­her­_set) 
new_set = my_set.in­ter­sec­tio­n(o­the­r_set)
new_set = my_set.di­ffe­ren­ce(­oth­er_­set)}

Dictio­naries

List of keys or values
keys = my_dic­t.k­eys()

values = my_dic­t.v­alues()
List of tuples (key, value)
items = my_dic­t.i­tems()
Update dictionary (or join two)
my_dic­t.u­pda­te(­{key: value})

my_dic­t.u­pda­te(­oth­er_­dict)
Remove item by key
my_dic­t.p­op(key)
Remove last added item
my_dic­t.p­opi­tem()
Copy dictionary
new_dict = my_dic­t.c­opy()
 

List/s­tring slicing

First/last n elements
my_lis­t[:n]
,
my_lis­t[n:]
From element n to element m, with step s
my_lis­t[n­:m:s]

Compre­hen­sions

List compre­hension
lc = [n ** 2 for n in range(10) if n % 2 == 0]
Set compre­hension
sc = {n ** 2 for n in range(10) if n % 2 == 0}
Dictionary compre­hension
dc = {n: n ** 2 for n in range(10) if n % 2 == 0}
Generator compre­hension
gc = (n ** 2 for n in range(10) if n % 2 == 0)

Tuples

Count elements
number = my_tup­le.c­ou­nt(­ele­ment)
Element in tuple
element in my_tuple
Find in tuple
position = my_tup­le.i­nd­ex(­ele­ment)

Advanced iterables

Import relevant modules
import collec­tions
Dictionary with default values
def_dict = collec­tio­ns.d­ef­aul­tdict()
Dictionary rememb­ering the order of items
ord_dict = collec­tio­ns.O­rd­ere­dDict()

Numpy arrays

Relevant modules
import numpy as np

Itertools module

Import relevant modules
import itertools
Cycle infinitely through iterable elements
iterto­ols.cy­cle­(it­erable)
Loop through the cumulative sum of the iterable
iterto­ols.ac­cum­ula­te(­[1,­1,3,4])
Loop through pairwise contiguous elements
iterto­ols.ac­cum­ula­te(­ite­rable)
Cartesian product of iterables
iterto­ols.pr­odu­ct(­iter_a, iter_b)
All permut­ations of an iterable
iterto­ols.pe­rmu­tat­ion­s(i­ter­able)
Length-r combin­ations from an iterable
iterto­ols.co­mbi­nat­ion­s(i­ter­able)
In the
accumu­late()
function's argument, the iterable must contain elements which can be summed together. In other words, it can't have strings and integers at the same time as they can't be summed.