I just realized a problem, when I try to use "+" in a list concatenation situation.
For example I can do:
print([[1] + [5]* n for n in range(1, 4)])
>>> [[1, 5], [1, 5, 5], [1, 5, 5, 5]]
But if I switch the sequence of the add function:
print([[5] * n for n in range(1, 4) + [1]])
I won't get:
>>> [[5, 1], [5, 5, 1], [5, 5, 5, 1]]
Instead I got:
TypeError: unsupported operand type(s) for +: 'range' and 'list'
Is there anyway to make it work?
You are doing this operation:
range(1, 4) + [1]
Which doesn't mean anything in this case.
You have to do the + [1] right after the [5] * n as in:
print([[5]*n+[1] for n in range(1, 4)])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With