transforming a list into a string

Tim Peters tim.peters at
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)]


> 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

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

More information about the Python-list mailing list