- Fast I/O — Python's default input() is too slow for massive test cases. You can override it with sys.stdin.readline to prevent Time Limit Exceeded (TLE) errors. import sys input = sys.stdin.readline
- Map function — While taking an integer array input in python we can easily take it through the map function and using the code mentioned below we can directly change it into a list. var = list(map(int,input().split())) n, k = map(int, input().split())
- Max-Min-Sum function — To get the maximum, minimum, or total sum value from a list without writing manual loops. val = max(var) total = sum(var)
- Sort — For sorting an array [Remember the sort() method doesn't return the sorted list. Rather it sorts the list in place and returns None. Therefore, assigning var.sort() in a variable will make it equal to None]. var.sort() print(var)
- List Comprehension — You can directly perform loops inside a list to initialize arrays in one clean line. lis = [i for i in range(10)] zeros = [0] * 10
- Array Output — For array output you can use two methods. 1. print((" ").join(map(str,array))) or simply print(*array)
- Recursion Limit — Python has a low default recursion depth limit. You must increase it to avoid a RecursionError when writing Depth First Search (DFS). import sys sys.setrecursionlimit(10*6)*
- Collections Module — Never use list.pop(0) as it takes $$$O(N)$$$ time. Use deque for fast appends and pops from both ends, or Counter for instant frequency counting. from collections import deque, Counter q = deque([1, 2, 3]) freq = Counter(var)
- Math Module — Don't write your own functions for basic math. Python has highly optimized built-in functions for GCD, LCM, ceilings, and floors. import math g = math.gcd(12, 18)
- Strings and Characters — Strings are immutable, so you cannot change characters directly. Use ord() and chr() to convert between characters and their ASCII integer values. pos = ord('d') — ord('a') rev_s = s[::-1]
- Bisect Module — Python's built-in module for performing Binary Search on sorted arrays instantly without writing manual loops. import bisect idx = bisect.bisect_left(var, 3)









Be very weary of using dicts. Due to hash collisions, inserting n items may take up to O(n^2) instead of O(n).
I saw Shiva Oswal at a grocery store in Pittsburgh yesterday. I told him how cool it was to meet him in person, but I didn’t want to be a douche and bother him and ask him for photos or anything. He said, “Oh, like you’re doing now?” I was taken aback, and all I could say was “Huh?” but he kept cutting me off and going “huh? huh? huh?” and closing his hand shut in front of my face. I walked away and continued with my shopping, and I heard him chuckle as I walked off. When I came to pay for my stuff up front I saw him trying to walk out the doors with like fifteen poker strategy guides in his hands without paying.The girl at the counter was very nice about it and professional, and was like “Sir, you need to pay for those first.” At first he kept pretending to be tired and not hear her, but eventually turned back around and brought them to the counter.When she took one of the bars and started scanning it multiple times, he stopped her and told her to scan them each individually “to prevent any hash colllizhins,” and then turned around and winked at me. I don’t even think that’s a word. After she scanned each bar and put them in a bag and started to say the price, he kept interrupting her by yawning really loudly.
Me too!
If anyone's interested in knowing how to prevent hash collisions in python, you can create a wrapper for integers with overloading for the
__hash__method for integers and add a random number (using random module or time module, depends on your mood) for the hash seeding. No need of such complexities for strings or most other data types since python already randomizes them automatically.Auto comment: topic has been updated by SpS__79 (previous revision, new revision, compare).
For the 3rd point, if you don't want to change the original list but store the sorted one in a separate variable, you can use new_list = sorted(old_list) Also for printing lists and other things, you can use any separator instead of the default " " by writing print(*list, sep="your_separator") or print(str, sep="your_separator")
Auto comment: topic has been updated by SpS__79 (previous revision, new revision, compare).