6

Based on some experiments, it appears to me that the following Python v2.7 code:

def lookup_pattern(pattern, file_containing_patterns):
  for line in file_containing_patterns:
    splits = line.split()
    if splits:
      if (pattern == splits[0]):
        return map(lambda x: x.strip(), splits[1:])
  return None

Could be simplified as follows to drop the map of strip():

def lookup_pattern(pattern, file_containing_patterns):
  for line in file_containing_patterns:
    splits = line.split()
    if splits:
      if (pattern == splits[0]):
        return splits[1:]
  return None

I believe this is true because the split() should remove all white space and thus the strip() would be a no-op.

Are there any cases where in the above two are not identical in effect, if so, what are they?

2
  • split() split a string followed by space, it doesn't remove white spaces. Mar 27, 2013 at 18:35
  • Eh? Sure it does. >>> 'foo '.split() -> ['foo'] Mar 27, 2013 at 19:53

1 Answer 1

6

The documentation indicates that split with the implicit whitespace delimiter (that is, split() without further arguments) will clear out any "empty tokens" and you don't need to strip anything. As any consecutive series of whitespace could be interpreted as a list of empty tokens delimited by space, that'll mean the strings get trimmed automatically.

If, instead you split with a different delimiter or implicitly defined the whitespace, this may happen:

' 1  2   3  '.split()
=> ['1', '2', '3']

'  1  2   3  '.split(None, 1)
=> ['1', '2   3  ']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.