Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!bloom-beacon.mit.edu!panix!gordon From: John Gordon Newsgroups: comp.lang.python Subject: Re: Python solve problem with string operation Date: Thu, 16 Jan 2014 22:58:44 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 32 Message-ID: References: NNTP-Posting-Host: panix3.panix.com X-Trace: reader1.panix.com 1389913124 8075 166.84.1.3 (16 Jan 2014 22:58:44 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Thu, 16 Jan 2014 22:58:44 +0000 (UTC) User-Agent: nn/6.7.3 Xref: csiph.com comp.lang.python:64113 In Mark Lawrence writes: > > input = "344111133311222223377" > > output = [] > > previous_ch = None > > for ch in input: > > if ch != previous_ch: > > output.append(ch) > > previous_ch = ch > > print ''.join(output) > > > Cheat, you've used a list :) Ack! I missed that the OP doesn't want to use lists. Well, let's try this instead: import sys input = "344111133311222223377" previous_ch = None for ch in input: if ch != previous_ch: sys.stdout.write(ch) previous_ch = ch sys.stdout.write('\n') -- John Gordon Imagine what it must be like for a real medical doctor to gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.