[Solved] Why is an array multiplied by [0] equal to the first element? [duplicate]


It is because [12, 2] is a list, and next notation: [0] or [1] is indexing.

You can test it if you try to print: print([12, 2][2]) you should get index out of range error.

EDIT: to answer your second question:

It is hard to say. target_f = self.model.predict(state) – it is some kind of structure and I can’t find information about this structure in the link you put above.

But we can consider some similar structure. Let’s say you have:

target_f = [{'action': 'value1', 'action2': 'valuex'}, {'action': 'value2', 'another_key': 'another_value'}]

In your code target_f[0][action] = target:

[0] is index in list. It stands for fist element of list: {'action': 'value1', 'action2': 'valuex'}

[action] is key in the dictionary. It stands for value1.

Since you are typing: target_f[0][action] = target thats mean you want to update your value1 by new value target. Your new stucture will looks like that:

target_f = [{'action': target, 'action2': 'valuex'}, {'action': 'value2', 'another_key': 'another_value'}]

3

solved Why is an array multiplied by [0] equal to the first element? [duplicate]