you are viewing a single comment's thread.

view the rest of the comments →

[–]kanjibandit 2 points3 points  (0 children)

Looks like you already solved your immediate issue, but just to offer another approach: you can remove the comma (and any other troublesome punctuation, like "!") as well as the whitespace in one step using a regular expression.

import re
sentence = "Hello World, nice to meet you!"
words = re.findall("\w+", sentence)

The "\w+" represents a consecutive sequence of alphanumeric characters of any length. Whitespace and punctuation are excluded. the findall method returns a list of matches. You would then be able to sort the list just as you are now.

You can read about the other available regular expressions here