how to join array of integers?

tokland at gmail.com tokland at gmail.com
Sat Sep 15 19:20:48 CEST 2007


Grant Edwards ha escrito:

> > Or, if you happen to like the itertools modules:
> >
> > from itertools import imap
> > ",".join(imap(str, [1, 2, 3]))
>
> It's nice people have invented so many ways to spell the
> builting "map" ;)

Did you wonder why the Python developers bother to implement "imap" if
"map" was already there?

> >>> ",".join(map(str,[1,2,3]))
> '1,2,3'

Of course the result is the same, but "map" returns a list while
"itertools.imap" returns an iterator. Fortunately, "map" will become
lazy on Py3000:

http://mail.python.org/pipermail/python-3000/2007-August/009207.html

As you said, it won't be noticeable for short lists, but it's a good
programming practice to use generators instead of lists (if you don't
really need a list!)

arnau




More information about the Python-list mailing list