[Tutor] Replace a character by index

Christian Witts cwitts at compuscan.co.za
Thu Jul 16 11:06:52 CEST 2009

Wayne wrote:
> Hi,
> My question is more about style/timing than anything else.
> In my program I'm taking a word and generating "blanks" in that word. 
> For example, the word cat could generate:
> _at
> c_t
> ca_
> I have two different ways I can put _ in the word:
> word = 'cat'
> ''.join(list(word)[1] = '_')
> and
> # I'm not using a constant, but randomly generating where the blank 
> appears
> word[:1] + '_' + word[1+1:]
> So, when I use the timeit module I get these results:
> In [78]: timeit.Timer("''.join(list('foo'))").timeit()
> Out[78]: 2.9940109252929688
> In [80]: timeit.Timer("'foo'[:2]+'_'+'foo'[2+1:]").timeit()
> Out[80]: 0.63733291625976562
> Quite a significant difference.
> So my question(s): Which method should I use/is more pythonic? Which 
> method do you/have you used? And the ubiquitous 'Why?'
> Normally I would lean towards the first method because reassigning a 
> value in a list seems more natural than string concatenation. In this 
> particular application I'm not exactly worried about performance - on 
> even an archaic computer I don't think one would notice.
> TIA for your input,
> Wayne
> -- 
> To be considered stupid and to be told so is more painful than being 
> called gluttonous, mendacious, violent, lascivious, lazy, cowardly: 
> every weakness, every vice, has found its defenders, its rhetoric, its 
> ennoblement and exaltation, but stupidity hasn’t. - Primo Levi
> ------------------------------------------------------------------------
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
Strings are essentially a list already of characters.  What would be 
slowing down your preferred method #1 would be your explicit cast to a 
list and then re-joining that list.  Strings support item assignment so 
you can save quite a few cycles just doing

word = 'cat'
word[1] = '_'

which would result in word printing 'c_t'.  I'm assuming your use case 
would be just a call to random with bounds zero -> length of string - 1 
and then using that index to replace the character with an underscore 
and it is much simpler and faster to just use the strings build in index 
and item assignment.

Hope that helps.

Kind Regards,
Christian Witts

More information about the Tutor mailing list