-3

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.

chadb768
  • 293
  • 1
  • 5
  • 15
  • 1
    `[[] self.size]` creates an array whose first element is an empty array, and second element is a number. – Barmar Mar 27 '17 at 21:49
  • 1
    Did you mean `[] * self.size`? – Barmar Mar 27 '17 at 21:49
  • for the underscore part, see : http://stackoverflow.com/questions/1739514/underscore-as-variable-name-in-python – WNG Mar 27 '17 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. – TigerhawkT3 Mar 27 '17 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. – chadb768 Mar 27 '17 at 21:55
  • @chadb768 there is no variable declaration in Python. And that isn't an array, that is a list. In fact, it is really a `SyntaxError`, so it's not even that... – juanpa.arrivillaga Mar 27 '17 at 22:04

2 Answers2

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]]
Richard
  • 44,865
  • 24
  • 144
  • 216
1
>>> 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.

Community
  • 1
  • 1
Alex Hall
  • 31,431
  • 4
  • 39
  • 71
  • 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? – Alex Hall Mar 27 '17 at 23:55