On Thu, Jul 16, 2009 at 4:29 AM, Wayne<sri...@gmail.com> 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] = '_')
Not in any Python I ever used... In [1]: word = 'cat' In [2]: In [3]: ''.join(list(word)[1] = '_') ------------------------------------------------------------ File "<ipython console>", line 1 SyntaxError: keyword can't be an expression (<ipython console>, line 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?' Um, the second method is faster and has the advantage of working :-) Seems like a no-brainer to me! Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor