Say I have the following lists:
a = 1
b = [2,3]
c = [4,5,6]
I want to concatenate them such that I get the following:
[1,2,3,4,5,6]
I tried the usual +
operator:
>>> a+b+c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'list'
This is because of the a
term. It is just an integer. So I convert everything to a list:
>>> [a]+[b]+[c]
[1, [2, 3], [4, 5, 6]]
Not quite what I'm looking for.
I also tried all the options in this answer, but I get the same int
error mentioned above.
>>> l = [a]+[b]+[c]
>>> flat_list = [item for sublist in l for item in sublist]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
TypeError: 'int' object is not iterable
It should be simple enough, but nothing works with that term a
. Is there any way to do this efficiently? It doens't necessarily have to be pythonic.