Search code examples
pythonconcatenation

Python - Can only concatenate list not float to list


I understand that I can only concatenate things of similar types, but I'm really confused as to why the following are of different types.

n = 100
table = [[0]*n for x in range(n)]
array1 = [[0] for i in range(n)]
mini = array1[1] + table[1][1]

I am trying to make mini store the integer that is the result of array1[1] and a table[1][1]'s value. But I get this error:

TypeError: can only concatenate list (not "float") to list

There must be something simple I'm missing. When I print just table[1][1] I get 0, so why is table[1][1] not treated as just 0 (I.e. 0 + 0)?


Solution

    • table[1] is indeed [0]*1 which is [0], and table[1][1] is indeed 0.

    • but array[1] is [0], which is a list

    • thus your attempt to do array1[1] + table[1][1] is actually [0] + 0

    To debug such things in the future, print each part of the expression the interpreter is complaining about:

    print(array1[1])
    print(table[1][1])