1

Hi I'm just working with adapting some python code and making sure I understand everything within it as I've never really worked with Python before. What does the [0] alone mean in the code mean? (qtable is 2 dimensional array, holding states(s) and actions(a), actions is a list, current_s is an integer)

self.q_table[current_s] = ([0] * len(self.actions))

1 Answers1

3

[0] is a list with one element: zero.

Then the program uses * operator (list multiplication in this case) to replicate the left operand list len(self.actions) times.

This creates a list of zeroes of len(self.actions) length.

With immutable types, that is the preferred initialization. A slower alternative (which must be used for mutable types) would be:

[0 for _ in len(self.actions)]
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Perfect thankyou, I thought it was just the value 0 and couldn't understand why it was being multiplied by that. Makes a lot of sense with the rest of the code now :) – TheButterWorm Mar 31 '20 at 10:52