Python Collections Cheat Sheet
1. Lists
Function / Method Example Output / Effect
len(list) len(fruits) Length of list
append(x) fruits.append('orange') Adds at end
insert(i, x) fruits.insert(1, 'kiwi') Insert at index
remove(x) fruits.remove('apple') Remove element
pop([i]) fruits.pop(0) Removes & returns element
clear() fruits.clear() Empty list
index(x) fruits.index('mango') Get index
count(x) fruits.count('apple') Count occurrences
sort() fruits.sort() Sort list
reverse() fruits.reverse() Reverse order
copy() new = fruits.copy() Copy list
2. Tuples
Function / Method Example Output
len(tuple) len(numbers) Length
count(x) numbers.count(2) Count occurrences
index(x) numbers.index(3) Find index
max(tuple) max(numbers) Max value
min(tuple) min(numbers) Min value
sum(tuple) sum(numbers) Sum of values
3. Dictionaries
Function / Method Example Output
len(dict) len(student) Length
get(key) student.get('age') Get value
keys() student.keys() All keys
values() student.values() All values
items() student.items() All items
update() student.update({'age':22}) Update dict
pop(key) student.pop('city') Remove key
popitem() student.popitem() Remove last
Function / Method Example Output
clear() student.clear() Empty dict
copy() new = student.copy() Copy dict
4. Sets
Function / Method Example Output
len(set) len(A) Length
add(x) A.add(10) Add element
remove(x) A.remove(2) Remove element
discard(x) A.discard(20) Safe remove
pop() A.pop() Remove random
clear() A.clear() Empty set
union() A|B Union
intersection() A&B Intersection
difference() A-B Difference
symmetric_difference() A^B Symmetric diff
issubset() {1,2}.issubset(A) Check subset
issuperset() A.issuperset({1,2}) Check superset
isdisjoint() A.isdisjoint({10}) Disjoint?