Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'from:addr:python': 0.09; 'def': 0.13; "'a'": 0.16; "(i'll": 0.16; 'comma': 0.16; "doesn't.": 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'handle,': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply- to:addr:python-list': 0.16; 'roy': 0.16; 'settle': 0.16; 'this:': 0.16; 'wrote:': 0.18; 'string,': 0.18; 'seems': 0.20; 'header:In- Reply-To:1': 0.22; 'handling': 0.30; "i've": 0.31; 'header:User- Agent:1': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'received:84': 0.34; 'reply-to:addr:python.org': 0.34; 'subject:How': 0.35; 'skip:" 10': 0.37; 'but': 0.37; 'list,': 0.37; 'some': 0.38; 'to:addr:python.org': 0.40; 'kind': 0.61; 'special': 0.68; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=JaQ+XD2V c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=9jsOeB20M3cA:10 a=HOvpTpyCj2EA:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=kr9JOmzmzdR3k2q4DoMA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Thu, 15 Dec 2011 17:27:14 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How to generate "a, b, c, and d"? References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> In-Reply-To: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323970009 news.xs4all.nl 6948 [2001:888:2000:d::a6]:39711 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17283 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] print(and_list([])) print(and_list(['a'])) print(and_list(['a', 'b'])) print(and_list(['a', 'b', 'c'])) print(and_list(['a', 'b', 'c', 'd']))