Python Forum
TypeError: can only concatenate list (not "str") to list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: can only concatenate list (not "str") to list
#1
Output:
lt1/forums /home/forums 1> py3 Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> a=[1,2,3]+'foobar' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate list (not "str") to list >>> a=[1,2,3] >>> a+='foobar' >>>
so, do you know what a now references? did you need to cheat?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Nice. It seems that the RHS can be any iterable in +=
>>> a = [1, 2, 3]
>>> a += (x+x for x in 'foobar')
>>> a
[1, 2, 3, 'ff', 'oo', 'oo', 'bb', 'aa', 'rr']
You can do it directly by using the list() constructor
>>> a = [1, 2, 3] + list('foobar')
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020