Your nums
list is probably a 1d list, let's say the list is:
nums = [1, 2, 3]
Then when you index the nums
list:
nums[i]
It will give you a value of 1
or 2
or 3
.
But single values can't be added to a list:
>>> [] + 1
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
[] + 1
TypeError: can only concatenate list (not "int") to list
>>>
But your second code works because adding a comma makes a single value into a tuple, like below:
>>> 1,
(1,)
>>>
So of course:
>>> res = []
>>> res += (1,)
>>> res
[1]
>>>
Works.
It doesn't only work with ,
comma, if you do:
>>> res = []
>>> res += [1]
>>> res
[1]
>>>
It will work too.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…