I don't understand how a boolean can by multiplied by a length. I'm fairly new to coding
def __init__(self, capacity, items):
self.currentSolution = [False]*len(items)
I don't understand how a boolean can by multiplied by a length. I'm fairly new to coding
def __init__(self, capacity, items):
self.currentSolution = [False]*len(items)
The notation [value] * number
builds a list containing value
at each index, with a length of number
Example
[False]*2 => [False, False]
[False]*10 => [False, False, False, False, False, False, False, False, False, False]
When you multiply a list by N it's actually creates a new list composed of N original lists.
Let me give you an example. When we'll use the following command:
[1, 2, 3] * 2
We'll get the following list:
[1, 2, 3, 1, 2, 3, 1, 2, 3]
So performing [False]*len(items) will actually create a list with the len of len(items) which every is False.
Another way to do the same thing could be:
[False for _ in range(len(items))]