formatting list -> comma separated (slightly different)

Duncan Booth duncan.booth at invalid.invalid
Wed Jul 9 17:06:08 EDT 2008


Michiel Overtoom <motoom at xs4all.nl> wrote:

> I occasionally have a need for printing lists of items too, but in the
> form: "Butter, Cheese, Nuts and Bolts".  The last separator is the
> word 'and' instead of the comma. The clearest I could come up with in
> Python is below. I wonder if there is a more pythonic solution for
> this problem.  Maybe something recursive?

>>> from itertools import chain, repeat
>>> def across(*iterables):
	iterables = map(iter, iterables)
	while 1:
		for it in iterables:
			yield it.next()

			
>>> friends=['Anne','Bob','Chris','Debbie','Eve','Fred']
>>> ''.join(across(friends, chain(repeat(', ', len(friends)-2), [' and 
'])))
'Anne, Bob, Chris, Debbie, Eve and Fred'

I feel there ought to be an easier way of alternating between iterators but 
if there is I can't think of it offhand. Also it breaks down unless you 
have at least two friends.

This version is a bit messy but seems to work reliably for any number of 
friends:

>>> for i in range(len(friends)+1):
	f = friends[:i]
	print ' and '.join(s for s in [', '.join(f[:-1]), ''.join(f[-1:])] if 
s)

	

Anne
Anne and Bob
Anne, Bob and Chris
Anne, Bob, Chris and Debbie
Anne, Bob, Chris, Debbie and Eve
Anne, Bob, Chris, Debbie, Eve and Fred

Of course neither works cleanly if you give it an iterator rather than a 
list. This is a bit longer but does work without knowing the length of the 
iterator in advance (and you could save a couple of lines by concatenating 
the adjacent yields):

>>> def commaand(it):
	it = iter(it)
	yield it.next()
	n = it.next()
	for nxt in it:
		yield ', '
		yield n
		n = nxt
	yield ' and '
	yield n

	
>>> for i in range(len(friends)+1):
	f = friends[:i]
	print ''.join(commaand(f))

	

Anne
Anne and Bob
Anne, Bob and Chris
Anne, Bob, Chris and Debbie
Anne, Bob, Chris, Debbie and Eve
Anne, Bob, Chris, Debbie, Eve and Fred



More information about the Python-list mailing list