transforming a list into a string

Tim Peters tim.peters at gmail.com
Sun Aug 1 02:16:02 CEST 2004


[Roy Smith]
> OK, so it sounds like you want to reverse() the list before the loop,
> then pop() items off the back.  Two O(n) passes [I'm assuming reverse()
> is O(N)]

Yes.

> beats O(n^2).

Absolutely.  Note that Peter Otten previously posted a lovely O(N)
solution in this thread, although it may be too clever for some
tastes:

>>> from itertools import izip
>>> items = ['1','2','7','8','12','13']
>>> it = iter(items)
>>> ",".join(["{%s,%s}" % i for i in izip(it, it)])
'{1,2},{7,8},{12,13}'
>>>



More information about the Python-list mailing list