Python Data Structures

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/31

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 6:15 PM on 7/5/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

32 Terms

1
New cards

Counter (collections)

Use when you need frequency counts or histograms. Built in O(n). Common for anagrams, duplicates, most common elements.

2
New cards

Counter Tradeoff

Extra memory for unique keys. Not ordered. Cleaner than manual dict counting.

3
New cards

defaultdict(list)

Use when grouping items by a key or building adjacency lists. O(1) per append.

4
New cards

defaultdict(list) Tradeoff

Automatically creates keys on access, which can hide bugs. Slight overhead vs dict.

5
New cards

deque (collections)

Use when adding/removing from both ends is required. Ideal for queues, BFS, sliding windows.

6
New cards

deque Tradeoff

No fast random access. Overkill if only one end is used.

7
New cards

heapq

Use when you must repeatedly find min or max efficiently. O(log n) per push/pop.

8
New cards

heapq Tradeoff

Not fully sorted. No fast arbitrary removal. Max-heap requires negation trick.

9
New cards

Problem says 'top K'

Use heapq to maintain K best elements instead of sorting everything.

10
New cards

Problem says 'group by category'

Use defaultdict(list) to map keys to lists.

11
New cards

Problem says 'count frequency'

Use Counter for clean and fast counting.

12
New cards

Problem says 'sliding window'

Use deque for O(1) window updates.

13
New cards

defaultdict

Use when grouping items into categories (e.g., grouping Jira tasks by Status) to avoid KeyError and manual initialization.

14
New cards

Counter

Use when you need to tally frequencies and immediately find the 'Top K' most frequent elements.

15
New cards

deque

Use for BFS (Breadth-First Search) or any scenario requiring O(1) additions/removals from the FRONT of a list.

16
New cards

heapq

Use when you have a dynamic stream of data and always need instant access to the minimum or maximum value.

17
New cards

set

Use for O(1) membership checks (e.g., 'Have I visited this node?') or to instantly remove duplicates.

18
New cards

defaultdict(list) vs dict

defaultdict(list) removes the need for 'if key not in d: d[key] = []', making code cleaner and faster to write.

19
New cards

deque(maxlen=N)

A specialized version of deque that automatically 'evicts' the oldest item when a new one is added beyond capacity N.

20
New cards

heapq (Max-Heap)

Python's heapq is a Min-Heap by default; to use it as a Max-Heap, multiply your values by -1 when pushing and popping.

21
New cards

Counter.most_common(k)

The specific method used to return a list of the 'k' most frequent elements and their counts.

22
New cards

Standard List (pop(0))

NEVER use this for removing the first item in an interview; it is O(N). Use deque.popleft() which is O(1).

23
New cards

defaultdict

Use when grouping items into categories (e.g., grouping Jira tasks by Status) to avoid KeyError and manual initialization.

24
New cards

Counter

Use when you need to tally frequencies and immediately find the 'Top K' most frequent elements.

25
New cards

deque

Use for BFS (Breadth-First Search) or any scenario requiring O(1) additions/removals from the FRONT of a list.

26
New cards

heapq

Use when you have a dynamic stream of data and always need instant access to the minimum or maximum value.

27
New cards

set

Use for O(1) membership checks (e.g., 'Have I visited this node?') or to instantly remove duplicates.

28
New cards

defaultdict(list) vs dict

defaultdict(list) removes the need for 'if key not in d: d[key] = []', making code cleaner and faster to write.

29
New cards

deque(maxlen=N)

A specialized version of deque that automatically 'evicts' the oldest item when a new one is added beyond capacity N.

30
New cards

heapq (Max-Heap)

Python's heapq is a Min-Heap by default; to use it as a Max-Heap, multiply your values by -1 when pushing and popping.

31
New cards

Counter.most_common(k)

The specific method used to return a list of the 'k' most frequent elements and their counts.

32
New cards

Standard List (pop(0))

NEVER use this for removing the first item in an interview; it is O(N). Use deque.popleft() which is O(1).