# Python for DSA

> Use Python with collections, heapq and bisect: the quickest language to write, fine for interviews but sometimes too slow for strict OAs. Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/dsa-placement/python-for-dsa
Last updated: 2026-11-28

Use Python with collections, heapq and bisect: the quickest language to write, fine for interviews but sometimes too slow for strict OAs.

Beginner · about 12 hours · difficulty 1/5

Learn first: [How Campus and Off-Campus Hiring Works](https://www.internhack.in/roadmaps/tracks/dsa-placement/placement-landscape)

Python lets you write short, readable solutions, which helps a lot in live interviews. The trade-off is speed: some OA problems with tight limits are hard to pass in Python.

### The subset you need

- Lists, tuples, dicts, sets, slicing and comprehensions.
- `collections`: `Counter`, `defaultdict`, `deque` (O(1) at both ends).
- `heapq`: a min-heap on a plain list. For a max-heap, push negated values.
- `bisect`: `bisect_left` and `bisect_right` for binary search on sorted lists.
- `sorted(arr, key=lambda x: (x[1], x[0]))` for multi-key sorts.
- Fast input: `sys.stdin.readline`.

### Pitfalls

- `list.pop(0)` and `list.insert(0, x)` are O(n). Use `deque`.
- Default recursion limit is about 1000. Deep DFS needs `sys.setrecursionlimit` or an iterative version.
- `[[0] * m] * n` makes n references to the same row. Use a comprehension.
- String concatenation in a loop is slow; collect parts and `"".join` them.
- If a platform offers PyPy, it is often several times faster for loop-heavy code.

You are ready to move on when you can use Counter, deque, heapq and bisect correctly without looking them up and know when to switch to an iterative solution.

## Key concepts

- **Lists, dicts, sets and slicing**
- **Counter and defaultdict**
- **deque**
- **heapq and max-heap trick**
- **bisect**
- **Recursion limit and iterative DFS**
- **PyPy vs CPython**

## Other options for this step

- [C++ and the STL](https://www.internhack.in/roadmaps/tracks/dsa-placement/cpp-stl): Use C++ with the Standard Template Library: the fastest and most common choice for placements and competitive programming.
- [Java and the Collections Framework](https://www.internhack.in/roadmaps/tracks/dsa-placement/java-collections): Use Java with ArrayList, HashMap, ArrayDeque and PriorityQueue: a strong choice if your college teaches Java or you target Java backend roles.

## Mini project

Solve 8 warm-ups in Python using only the standard library: word frequency with Counter, sliding window max with deque, top k frequent with heapq, search insert position with bisect, group anagrams with defaultdict, BFS on a grid, merge intervals and running median with two heaps.

## Check yourself

Why is using list.pop(0) inside a BFS loop a performance bug, and what should you use instead?

## Free resources

- [Python docs: collections](https://docs.python.org/3/library/collections.html) (Python)
- [Python docs: heapq](https://docs.python.org/3/library/heapq.html) (Python)
- [Python docs: bisect](https://docs.python.org/3/library/bisect.html) (Python)

Previous: [Java and the Collections Framework](https://www.internhack.in/roadmaps/tracks/dsa-placement/java-collections) · Next: [Time and Space Complexity](https://www.internhack.in/roadmaps/tracks/dsa-placement/complexity-analysis)

## More in Getting Started: Placements, Language and Complexity

- [How Campus and Off-Campus Hiring Works](https://www.internhack.in/roadmaps/tracks/dsa-placement/placement-landscape)
- [C++ and the STL](https://www.internhack.in/roadmaps/tracks/dsa-placement/cpp-stl)
- [Java and the Collections Framework](https://www.internhack.in/roadmaps/tracks/dsa-placement/java-collections)
- [Python for DSA](https://www.internhack.in/roadmaps/tracks/dsa-placement/python-for-dsa)
- [Time and Space Complexity](https://www.internhack.in/roadmaps/tracks/dsa-placement/complexity-analysis)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DSA and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/dsa-placement)
