Hash Table

The hash table uses a hashing function to efficiently store and retrieve data, It is also known as a hash map, map, dictionary, or associative array. An underlying array is used to store the data, the hashing function will take a key and calculate its index in the array. The hash function must be: consistent […]

Breadth-First Search

The Breadth-First Search answers two questions: is there a path from node a to node b? what is the shortest path from node a to node b? It is a graph algorithm. Graphs are made up of nodes and edges: Nodes are connected to other nodes called their neighbors. A directed graph is a node […]

Iterable in Python

Iterable An iterable is any Python object capable of returning its members one at a time, permitting it to be iterated over in a for-loop, such as lists, tuples, sets and strings – as well as generators. An iterable object implements the __getitem__ and __len__ methods. An object can also implement the __iter__ method to […]

Function Argument Unpacking

Putting a * operator before a tuple, list or generator, in a function call will unpack it and pass its elements as separate positional arguments to the called function. Similarly, the ** operator will unpack keyword arguments from dictionaries and pass them to the called function. Another example: You may need to unpack specific values […]


Quicksort uses a divide and conquer approach to sorting an array, and leverages recursion. Essentially, you pick a pivot number from the array to be sorted, it can be any number: [4, 6, 2, 1, 5] Let’s pick 2. Now partition the array into two sub-arrays, the numbers less than or equal to the pivot […]

Set Comprehensions

Set comprehensions are similar to list comprehensions, the only difference is the use of braces {} rather than brackets [] and sets can only contain unique values. They are of the form: my_set = { expression for expression in iterable } You can also add an if condition: my_set = { expression for expression in […]

Dictionary Comprehensions

Like the venerable list, dictionaries are extremely useful data structures in Python, and used extensively. Similar to lists, we can build dictionaries from existing collections. The blueprint is: my_dict = { key_expression : value_expression for expression in iterable } This example should make things clearer: Another simple example: Just remember you need to select a […]

Sets in Python

The set type is a really useful data structure, it cannot contain duplicate values. It is an unordered collection of objects. It supports: in not in for i in set len() all() any() enumerate() min() max() sorted() sum() Python sets also support mathematical operations including: Subset (x < y) Intersection (x & y) Union (x […]

List Comprehensions

Whenever you see this pattern to create a list of items: (values) = [] for (value) in (collection): (values).append((expression)) You can replace it with a list comprehension, which takes the form of: (values) = [ (expression) for (value) in (collection)] This: Can be replaced with: Notice how the variable used to iterate over the collection […]

Magic Methods

Python has a number of special methods, also known as dunder methods as they begin (and end) with a double underscore. Also called magic methods, they enable many of Python’s built-in functions to operate on your objects. These methods are not meant to be invoked directly. For example, the __init__ special method used to initialise […]