[Tutor] Different idioms for making lists of strings

python python <python@inkedmn.net>
Thu, 5 Sep 2002 16:55:32 -0700


Scot,

you can also create lists using loops:

>>> oldlist
[1, 2, 3, 4, 5]
>>> newlist
[]
>>> for item in oldlist:
...     newlist.append(item)
...
>>> newlist
[1, 2, 3, 4, 5]

brett


SWS> Hello there, 

SWS> While working my way thru the "Python Cookbook", I have come across a 
SWS> couple of examples where lists of strings are created not the "normal" way

SWS> alist = ['From', 'To', 'Subject']

SWS> but by splitting up a string

SWS> alist = 'From To Subject'.split()

SWS> which of course is a lot easier to type, the computation involved should be 
SWS> trivial, and the days when you would have had to import the string module 
SWS> to do stuff like this are long over. So I thought I'd point it out to the 
SWS> rest of the crowd here. 

SWS> Y, Scot