Python Forum
can only concatenate list (not "int") to list? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: can only concatenate list (not "int") to list? (/thread-1621.html)



can only concatenate list (not "int") to list? - landlord1984 - Jan-17-2017

I can do:
[1, 2, 3] + [4, 5, 6]
But I cannot do:
nums=[1, 2, 3]
nums[1:]+nums[0]
TypeError: can only concatenate list (not "int") to list


Why?


RE: can only concatenate list (not "int") to list? - Larz60+ - Jan-17-2017

see: http://c4m.cdf.toronto.edu/cohort1/winterphase/level3/ListsStringsLoopsFunctions.html

Specifically In [4]:

not with a slice, but same thing.

essentially nums[0] is considered an int, not a list
concatenate must be list + list, not list + int

so the following works:
n = [1,2,3,4,5]
n1 = n[3:]+n[4:]
print(n1)



RE: can only concatenate list (not "int") to list? - landlord1984 - Jan-17-2017

(Jan-17-2017, 05:38 AM)Larz60+ Wrote: n = [1,2,3,4,5]
n1 = n[3:]+n[4:]
print(n1)

Thanks. I tried out a better way:nums[1:]+[nums[0]]