[Tutor] interesting str behavior ... (fwd)

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri May 16 13:49:01 2003


> > If we want to make temp 56 elements long, and if we want to do it by
> > using append() only, we've got to do something to let us do a repeated
> > append() that many times.
> >
> Re: And that is as easy as rearranging the contruct
>
> >>> temp.append([item for item in letters])
>
> like so:
>
> >>> junk_this = [temp.append(item) for item in letters]
> >>> temp
> ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
> 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
> 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
> 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']


Hello!


Although this works, there's a simpler way to do things, without list
comprehensions:


###
for item in letters:
   temp.append(item)
###


In our enthusiasm for list comprehensions, let's not forget about loops.
*grin*




Good luck!