Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!txtfeed1.tudelft.nl!tudelft.nl!txtfeed2.tudelft.nl!amsnews11.chello.com!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'mrab': 0.04; 'received:verizon.net': 0.07; 'terry': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'output': 0.10; 'def': 0.13; '"and': 0.16; "'a'": 0.16; "(i'll": 0.16; 'comma': 0.16; "doesn't.": 0.16; 'handle,': 0.16; 'input,': 0.16; 'reedy': 0.16; 'roy': 0.16; 'settle': 0.16; 'this:': 0.16; 'wrote:': 0.18; 'string,': 0.18; 'jan': 0.19; 'seems': 0.20; 'header:In-Reply- To:1': 0.22; 'slice': 0.23; 'string': 0.24; 'pm,': 0.29; 'handling': 0.30; "i've": 0.31; 'list': 0.32; 'header:User- Agent:1': 0.33; 'header:X-Complaints-To:1': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'copying': 0.35; 'subject:How': 0.35; 'skip:" 10': 0.37; 'two': 0.37; 'but': 0.37; 'list,': 0.37; 'received:org': 0.38; 'some': 0.38; 'created': 0.38; 'to:addr:python.org': 0.40; 'kind': 0.61; 'making': 0.67; 'special': 0.68; '12:27': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: How to generate "a, b, c, and d"? Date: Thu, 15 Dec 2011 19:57:22 -0500 References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA2DF2.9080107@mrabarnett.plus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: <4EEA2DF2.9080107@mrabarnett.plus.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323997066 news.xs4all.nl 6899 [2001:888:2000:d::a6]:41899 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17322 On 12/15/2011 12:27 PM, MRAB wrote: > On 15/12/2011 16:48, Roy Smith wrote: >> I've got a list, ['a', 'b', 'c', 'd']. I want to generate the string, >> "a, b, c, and d" (I'll settle for no comma after 'c'). Is there some >> standard way to do this, handling all the special cases? >> >> [] ==> '' >> ['a'] ==> 'a' >> ['a', 'b'] ==> 'a and b' >> ['a', 'b', 'c', 'd'] ==> 'a, b, and c' >> >> It seems like the kind of thing django.contrib.humanize would handle, >> but alas, it doesn't. > > How about this: > > def and_list(items): > if len(items) <= 2: > return " and ".join(items) > > return ", ".join(items[ : -1]) + ", and " + items[-1] To avoid making a slice copy, last = items.pop() return ", ".join(items) + (", and " + last) I parenthesized the last two small items to avoid copying the long string twice with two appends. Even better is items[-1] = "and " + items[-1] return ", ".join(items) so the entire output is created in one operation with no copy. But I would only mutate the list if I started with items = list(iterable) where iterable was the input, so I was mutating a private copy. -- Terry Jan Reedy