Python Cheat sheet
Python Cheat Sheet for LeetCode
LeetCode is a popular platform for practicing coding problems and preparing for technical interviews. I’ve created this cheat sheet to help quickly reference common data structures, algorithms, and Python APIs used in LeetCode problems.
Lists
1 | myList = [1, 'abc'] # lists can contain different data types |
Useful Tricks
1 | # slicing |
Dictionary (dict)
1 | myDict = {'a': 1, 'b': 2} |
Set
1 | mySet = {1, 2, 3} |
Counter
1 | from collections import Counter |
Deque
1 | from collections import deque |
Heap (Priority Queue)
1 | import heapq |
Final Tips
Use list for most problems
Use set/dict for O(1) lookup
Use deque for BFS
Use heapq for top K problems
Use Counter for frequency problems
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.





