Question about 'for' loop

Carsten Haese carsten at uniqsys.com
Fri Aug 17 20:42:53 EDT 2007


On Fri, 2007-08-17 at 17:45 -0500, Robert Dailey wrote:
> [...]
> Secondly, I'm wondering how I can use this method of a for loop to
> append strings to strings in a list. For example:
> 
> mylist = [
> "Hello ",
> "Hello again "
> ]
> 
> I should be able to do this: 
> 
> print [ i + "World" for i in mylist ]
> 
> Which should yield the output:
> 
> ["Hello World", "Hello again world"]
> 
> However, instead I get an error message saying "TypeError: cannot
> concatenate 'str' and 'list' objects" 
> 
> How can I achieve the above? Thanks for reading.

You must have done something different than what you're describing above
to get that error message. The code you posted works as expected:

>>> mylist = [
... "Hello ",
... "Hello again "
... ]
>>> print [ i + "World" for i in mylist ]
['Hello World', 'Hello again World']

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list