Welcome, guest | Sign In | My Account | Store | Cart

Python allows some degree of functional programming. Using the reduce() function and the lambda form we can express the common tab ->spaces expansion routine in a simpler and more efficient way

Python, 6 lines
# Amazing quick
def expandtab(str,tab=8):
   return reduce(lambda a,b: 
                      a + ' '*(tab-len(a)%tab) + b,

Python users often forget the power of functional approaches. This recipe show how easy and fast a task can be if you just leave the classical imperative style (and even shows a possible use for reduce().

We don't need to use all the power of regexen, neither we need to explicitly process lists, reduce() handles quite everything for us.

Each element of the list resulting from str.split() got passed to the lambda function. The other argument to the lambda is the result from the previous computation. So at the first pass we translate a '\t' to spaces, than concatenate it to a newer piece and pass it to the next iteration. This way we expand each tab in turn, with a simple single function.

Note that even if everything was expressed as a single logical line, using lambda, you can extract that as an expand_single_tab(str1,str2,tablen) function.


Jason Orendorff 17 years, 9 months ago  # | flag

Built-in expandtabs() method. Readers should recall that string objects have an expandtabs() method that does this. There's no need to write real code like the above.

>>> '\t\tfoo'.expandtabs()
'                foo'
>>> '\t\tfoo'.expandtabs(2)
'    foo'
Raymond Hettinger 17 years, 9 months ago  # | flag

Functional yes, but not a great teaching example. * Avoid using str as a variable name

  • Join strings with ''.join() instead of +

  • Lambdas are much slower than doing computations in a for-loop

Created by gabriele renzi on Sun, 8 Feb 2004 (PSF)
Python recipes (4591)
gabriele renzi's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks