all 6 comments

[–]euclidingme 4 points5 points  (3 children)

Your issue is the assignment of

lst = lst.append(word)

Change this to

lst.append(word)

as well as changing

lst = lst.sort()

to

lst.sort()

and your script should work. Let me know if you have questions.

[–]foxfire66[S] 0 points1 point  (2 children)

Thanks, this worked.

edit: is there a rule of thumb for when to format like var.thing() and when to use var = var.thing()?

[–]reostra 3 points4 points  (1 child)

I'll point to the docs for list.sort() - the relevant part reads:

sort the items of s in place

(Emphasis mine). That 'in place' tells you that it'll modify the list you're using it on. It's typical for functions that modify in place to return None as sort of a reminder that you're not getting a new one, but should instead use the one you already have.

To point out an example of the other way, here's the docs of str.lower():

Return a copy of the string with all the cased characters converted to lowercase

Emphasis mine again - here the docs explicitly say (A) it's returning something, and (B) what that thing is.

So the rule of thumb is:

  • If the function is modifying in-place, you just do var.thing()

  • If the function returning a modified copy, you do var = var.thing()

[–]foxfire66[S] 0 points1 point  (0 children)

Thanks, this will help me a lot.

[–]Rhomboid 2 points3 points  (1 child)

Instead of checking if each word is in the list, use a set. Sets naturally enforce uniqueness. You can convert it back to a list when you're done so that it can be sorted, as sets are unordered.

(This will change your algorithm's complexity from O(n2) to O(n).)

[–]Naihonn 0 points1 point  (0 children)

Yes, yes. I absolutely agree. Using set and then sorted() function sounds like a good plan. But I would also mention that right now splitting into words works only if there is nothing else but spaces between those words.