Sort a list in Python My aim is to sort a list of strings where words have to be sorted alphabetically. Words starting with s should be at the start of the list (they should be sorted as well), followed by the other words.

The below function does that for me.

def mysort(words):
    mylist1 = sorted([i for i in words if i[:1] == "s"])
    mylist2 = sorted([i for i in words if i[:1] != "s"])
    list = mylist1 + mylist2
    return list


I am looking for better ways to achieve this, or if anyone can find any issues with the code above.
Some notes about your code:


sorted([i for i in words if i[:1] == "s"]): No need to generate an intermediate list, drop those [ ] and you'll get a lazy generator expression instead.
mylist1. This my prefix looks pretty bad. Do other languages use them?
list: Red flag. Don't overshadow a built-in (at least not one so important) with a variable.
return list. You can directly write return mylist1 + mylist2, no gain in naming the value you're about to return (the name of the function should inform about that).
i[:1] == "s" -> i.startswith("s").


Now what I'd write. Using sorted with the argument key (Schwartzian transform) and taking advantage of the lexicographical order defined by tuples:

def mysort(words):
    return sorted(words, key=lambda word: (0 if word.startswith("s") else 1, word))


If you are familiar with the fact that False &lt; True, it can be simplified a bit more:

sorted(words, key=lambda word: (not word.startswith("s"), word))


Here's another way using the abstraction partition:

words_with_s, other_words = partition(words, lambda word: word.startswith("s"))
sorted(words_with_s) + sorted(other_words)


Writing partition is left as an exercise for the reader :-)