Skip to content

What does this line of code do when initializing a hash table in python?

An answer to this question on Stack Overflow.

Question

I'm looking at the solution for how to create a hash table in python and came across this function:

def __init__(self, size):
    self.size = size
    self.table = [[] for _ in range(self.size)]

The third line of code creating the multi-dimensional array is confusing me. Mainly this part:

for _ in range(self.size)

What exactly is this doing? And why is it needed instead of just doing something like this:

[[] self.size]

Which would create a 2-dimensional array with the size of self.size correct? Any sort of visual aids would really help too.

Answer

The line

self.table = [[] for _ in range(self.size)]

creates an array of buckets for holding the contents of the hash table.

The variable name _ is used to indicate that the variable doesn't matter and its contents are, essentially, thrown away. This is useful for unused variables with a short scope, like this one.

You suggest initializing things like this:

self.table = [[]]*self.size

But this is a bad idea because you actually get self.size copies of the same list! That is:

a=[[]]*4
>>> [[], [], [], []]
a[0].append(3)
>>> [[3], [3], [3], [3]]