List of Useful Python Tricks for Solving Coding Questions
Saad Bash
Python is great language choice when solving DSA questions because of its english-like syntax, rich set of built-in libraries.
Here is list of some helpful methods and tricks when solving DSA questions.
collections.Counter()
- Elements as keys, and their counts are stored as values.
nums = [1,1,2,3,4,5,3,2,3,4,2,1,2,3]
counter = collections.Counter(nums)
print(counter)
# Output: Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1})reversed(range)
for i in reversed(range(5)):
print(i)
# Output
# 4
# 3
# 2
# 1
# 0
# Iterate list in reversed order
my_list = [1, 2, 3]
for i in reversed(range(len(my_list))):
print(my_list[i])
# Output
# 3
# 2
# 1defaultdict(set) and defaultdict(list)
defaultdict is a subclass of the built-in dict class in Python.
The functionality of both dictionaries and defaultdict are almost same except for the fact that defaultdict never raises a KeyError.
Examples:
from collections import defaultdict
# Create a defaultdict with a set as the default factory function
d = defaultdict(set)
# Add some values to the defaultdict
d['key1'].add('value1')
d['key1'].add('value2')
d['key2'].add('value3')
# Access a key that doesn't exist yet => No KeyError
print(d['key3']) # Output: set()
# Access an existing key
print(d['key1']) # Output: {'value1', 'value2'}from collections import defaultdict
# Create a defaultdict with a list as the default factory function
d = defaultdict(list)
# Add some values to the defaultdict
d['key1'].append('value1')
d['key1'].append('value2')
d['key2'].append('value3')
# Access a key that doesn't exist yet
print(d['key3']) # Output: []
# Access an existing key
print(d['key1']) # Output: ['value1', 'value2']Create empty list of certain size
Creating an empty list
l = [None] * 10With zeros
x = [0] * 5
collections.deque([iterable, [maxlen]]) - Doubly Ended Queue
- For faster
O(1)append and pop operations on both ends
queue = collections.deque([1, 2, 3])
## Operations
queue.append(4)
queue.appendleft(0)
queue.pop() # Returns 4
queue.popleft() # Returns 0enumerate with optional start argument
Specify the starting index for the keys (indexes) in the enumerate object
name="saad"
for i, c in enumerate(name,start=1):
print(f'{i}->{c}')
# Output
# 1->s
# 2->a
# 3->a
# 4->dString strip()
strip() method removes any leading and trailing whitespaces from a given string.
text = " Hello, World "
stripped = text.strip()
print(stripped) # "Hello, World"String isalnum()
isalnum() method returns True if all characters in the string are alphanumeric (either alphabets or numbers). If not, it returns False.
print(" ".isalnum()) # False
x = "123"
print(x.isalnum()) # True
y = "hello1"
print(y.isalnum()) # TrueCreate Positive & Negative Infinity
# Positive
x = float('inf')
# Negative
y = -float('inf')
# OR using math module
import math
x = math.inf
y = -math.inf