Lists, tuples, dictionaries, sets — methods, operations, and performance tips
append, extend, insert — add items to a list
lst = [1, 2, 3]
lst.append(4) # [1, 2, 3, 4]
lst.extend([5, 6]) # [1, 2, 3, 4, 5, 6]
lst.insert(0, 0) # [0, 1, 2, 3, 4, 5, 6]remove, pop, clear — delete items from a list
lst.remove(3) # removes first occurrence of 3
lst.pop() # removes & returns last item
lst.pop(0) # removes & returns item at index 0
lst.clear() # removes all itemsSort in-place or get sorted copy, find items
lst.sort() # in-place sort
lst.sort(reverse=True) # reverse sort
sorted(lst) # returns new sorted list
lst.index(5) # first index of value 5
lst.count(3) # count occurrences of 3Tuples are immutable ordered sequences
t = (1, 2, 3)
x, y, z = t # unpack
first, *rest = t # extended unpack: first=1, rest=[2,3]
t.index(2) # index of value 2
t.count(1) # count of value 1Tuples with named fields for readability
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)
print(p.x, p.y) # 3 4Safely access and modify dictionary values
d = {"a": 1, "b": 2}
d.get("c", 0) # 0 (default if missing)
d.setdefault("c", 3) # sets "c":3 if not present
d.update({"d": 4}) # merge another dict
d.pop("b") # remove and return valueIterate over keys, values, or key-value pairs
for key in d: # iterate keys
for val in d.values(): # iterate values
for k, v in d.items(): # iterate pairsAdd, remove, and check membership in a set
s = {1, 2, 3}
s.add(4) # add one element
s.remove(2) # remove (raises KeyError if missing)
s.discard(99) # remove if present (no error)
5 in s # FalseUnion, intersection, difference, symmetric difference
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
a | b # union: {1,2,3,4,5,6}
a & b # intersection: {3,4}
a - b # difference: {1,2}
a ^ b # symmetric diff: {1,2,5,6}Count hashable objects and find most common
from collections import Counter
c = Counter("mississippi")
# Counter({'s': 4, 'i': 4, 'p': 2, 'm': 1})
c.most_common(2) # [('s', 4), ('i', 4)]Dictionary that provides a default value for missing keys
from collections import defaultdict
graph = defaultdict(list)
graph["A"].append("B") # no KeyError
graph["A"].append("C")
# {"A": ["B", "C"]}Double-ended queue — O(1) appends and pops from both ends
from collections import deque
dq = deque([1, 2, 3])
dq.appendleft(0) # [0, 1, 2, 3]
dq.append(4) # [0, 1, 2, 3, 4]
dq.popleft() # returns 0
dq.rotate(1) # [4, 1, 2, 3]Dict that remembers insertion order (useful pre-Python 3.7)
from collections import OrderedDict
od = OrderedDict()
od["first"] = 1
od["second"] = 2
od.move_to_end("first") # move to endCount frequencies and find top items
from collections import Counter
words = "the cat sat on the mat the cat".split()
freq = Counter(words)
print(freq.most_common(3))
# [('the', 3), ('cat', 2), ('sat', 1)]
# Counter arithmetic
a = Counter("aab")
b = Counter("abc")
print(a + b) # Counter({'a': 3, 'b': 2, 'c': 1})
print(a - b) # Counter({'a': 1})Group items by a key without KeyError boilerplate
from collections import defaultdict
# Group words by first letter
words = ["apple", "ant", "banana", "bear", "cherry"]
by_letter = defaultdict(list)
for word in words:
by_letter[word[0]].append(word)
# {"a": ["apple", "ant"], "b": ["banana", "bear"], "c": ["cherry"]}
# Nested defaultdict
tree = defaultdict(lambda: defaultdict(int))
tree["fruits"]["apple"] += 1Efficient FIFO queue using deque
from collections import deque
# BFS traversal with deque
def bfs(graph, start):
visited = set()
queue = deque([start])
visited.add(start)
result = []
while queue:
node = queue.popleft()
result.append(node)
for neighbor in graph[node]:
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
return resultUse sets for O(1) membership testing — much faster than x in list for large collections
Prefer dict.get(key, default) over dict[key] to avoid KeyError
Use tuple for data that should not change — it is hashable and can be used as a dict key
Use deque instead of list when you need frequent appends/pops from both ends
Counter is excellent for frequency analysis and histogram building