-2

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.

6
  • 1
    [[] self.size] creates an array whose first element is an empty array, and second element is a number. Commented Mar 27, 2017 at 21:49
  • 1
    Did you mean [] * self.size? Commented Mar 27, 2017 at 21:49
  • for the underscore part, see : stackoverflow.com/questions/1739514/… Commented Mar 27, 2017 at 21:50
  • 1
    [[] self.size] is a syntax error. [[], self.size] would be a list containing an empty list and whatever self.size is. Commented Mar 27, 2017 at 21:53
  • Okay I was mistaken thinking that [[] self.size] was declaring an array of self.size size with each index containing an empty array. Thanks for clearing that up. Commented Mar 27, 2017 at 21:55

2 Answers 2

2
>>> good_table = [[] for _ in range(5)]
>>> good_table
[[], [], [], [], []]
>>> good_table[0].append(3)
>>> good_table
[[3], [], [], [], []]
>>> suggested_table = [[] 5]
  File "<input>", line 1
    suggested_table = [[] 5]
                          ^
SyntaxError: invalid syntax
>>> suggested_table = [[] * 5]
>>> suggested_table
[[]]
>>> [] * 5
[]
>>> bad_table = [[]] * 5
>>> bad_table
[[], [], [], [], []]
>>> bad_table[0].append(3)
>>> bad_table
[[3], [3], [3], [3], [3]]

For the last part, see List of lists changes reflected across sublists unexpectedly.

Sign up to request clarification or add additional context in comments.

1 Comment

I would love to know why people are downvoting this. What information do I need to add? What hasn't already been thoroughly covered elsewhere?
1

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]]

4 Comments

Of the same list.
@juanpa.arrivillaga: Quite right. I've fixed the mistake.
Yeah, sorry for coming off as pedantic, but it's a pet peeve of mine. Python lists are different than arrays, especially arrays in languages like C++ and Java. They are heterogeneous, resizable array-lists with constant-time append/extend operations.
@juanpa.arrivillaga: It was fuzzy wording on my part, and I appreciate the reminder that these are inherently different data structures.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.