Python: My Favorite Python Tricks for LeetCode Questions
Briefly

For instance, if you just google 'heapq', you'll see the official docs for heapq, which are often enough. However, it's also helpful to sometimes just quickly get help() in the shell. Here, I can't remember how to insert works: >>> help([]) >>> dir([]) >>> help([].insert)
If you need to loop over a list, you can use enumerate() to get both the item as well as the index. As a mnemonic, I like to think for (i, x) in enumerate(...): for (i, x) in enumerate(some_list): ...
Remember, when you use [] with a dict, if the value doesn't exist, you'll get a KeyError. Rather than see if an item is in the dict and then look up its value, you can use get(): val = some_dict.get(key) # It defaults to None. if val is None: ...
Have you switched to Python's new format strings yet? They're nicer than % and .format(): print(f'Got item: {item}')
Read at Jjinux
[
|
]