Python – Can only concatenate list not float to list
Question:
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)?
Answers:
-
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])
Look at what array1
contains:
>>> array1[1]
[0]
It’s not a list filled with zeroes, it’s a list filled with a bunch of lists, each of them containing a single zero.
you probably want this line:
array1 = [[0] for i in range(n)]
to be this:
array1 = [0 for i in range(n)]
Both table
and array1
are lists of lists of integers (0 in this case). But
array1[1]
is a list of integers while
table[1][1]
is an integer. You could solve the problem using a slice, like this:
mini = array1[1] + table[1][1:2]
which results in mini
being equal to
[0, 0]
The slice table[1][1:2]
is itself a list containing a single element (table[1][1]
), and since concatenating lists is allowed, it works.
Your variable array1
is a list containing lists that each only have a single element, 0, for i in range(n)
. That is, array1[1] = [0]
, not array1[0] = 0
. That’s why the types mismatch.
Also, don’t forget that Python is 0-based, not 1-based. It doesn’t cause an error here, but seems to betray your thinking of [1] as the first slot in the list, when [0] is.
When you print each operand, you will get the following output:
>>> array1[1]
[0]
>>> table[1][1]
0
So the first is a list, and the second is a single integer. As such it obviously fails.
So you either need to use array1[1][0]
to add the integer values, or use table[1]
to concatenate lists.
so why is table[1][1]
not treated as just 0
It is – but 0
cannot be added to array1[1]
with +
. It’s exactly as the error message says. +
is used to add two list
s together:
[1, 2, 3] + 4 # WRONG: can't concatenate a non-list with a list
[1, 2, 3] + [4] # RIGHT: concatenates the two lists, producing [1, 2, 3, 4]
If array1[1]
is intended to be a float
rather than a list
, then array1
should store numbers rather than lists. The code array1 = [[0] for i in range(n)]
makes each element in array1
be [0]
, i.e. a list.
That said, please remember that array indexes start with 0.
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)?
-
table[1]
is indeed[0]*1
which is[0]
, andtable[1][1]
is indeed0
. -
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])
Look at what array1
contains:
>>> array1[1]
[0]
It’s not a list filled with zeroes, it’s a list filled with a bunch of lists, each of them containing a single zero.
you probably want this line:
array1 = [[0] for i in range(n)]
to be this:
array1 = [0 for i in range(n)]
Both table
and array1
are lists of lists of integers (0 in this case). But
array1[1]
is a list of integers while
table[1][1]
is an integer. You could solve the problem using a slice, like this:
mini = array1[1] + table[1][1:2]
which results in mini
being equal to
[0, 0]
The slice table[1][1:2]
is itself a list containing a single element (table[1][1]
), and since concatenating lists is allowed, it works.
Your variable array1
is a list containing lists that each only have a single element, 0, for i in range(n)
. That is, array1[1] = [0]
, not array1[0] = 0
. That’s why the types mismatch.
Also, don’t forget that Python is 0-based, not 1-based. It doesn’t cause an error here, but seems to betray your thinking of [1] as the first slot in the list, when [0] is.
When you print each operand, you will get the following output:
>>> array1[1]
[0]
>>> table[1][1]
0
So the first is a list, and the second is a single integer. As such it obviously fails.
So you either need to use array1[1][0]
to add the integer values, or use table[1]
to concatenate lists.
so why is
table[1][1]
not treated as just0
It is – but 0
cannot be added to array1[1]
with +
. It’s exactly as the error message says. +
is used to add two list
s together:
[1, 2, 3] + 4 # WRONG: can't concatenate a non-list with a list
[1, 2, 3] + [4] # RIGHT: concatenates the two lists, producing [1, 2, 3, 4]
If array1[1]
is intended to be a float
rather than a list
, then array1
should store numbers rather than lists. The code array1 = [[0] for i in range(n)]
makes each element in array1
be [0]
, i.e. a list.
That said, please remember that array indexes start with 0.