Skip to content

Instantly share code, notes, and snippets.

@matiskay
Created January 10, 2012 22:40
Show Gist options
  • Save matiskay/1591655 to your computer and use it in GitHub Desktop.
Save matiskay/1591655 to your computer and use it in GitHub Desktop.
Refactoring the clean function
# After
def clean(i_list):
"""
Generate a string from a list stripped all the extra whitespace
"""
r_list = [string.strip() for string in i_list]
return ' '.join(r_list)
# Before
def clean(i_list):
r_list = []
for x in i_list:
pattern = re.compile(r'\s+')
x = re.sub(pattern, ' ', x)
x = x.strip()
r_list.append(x)
return ' '.join(r_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment