You could do it in one line, with:

sorted(words, key=lambda x: 'a' + x if x.startswith('s') else 'b' + x)

The sorted() function takes a keyword argument key, which is used to translate the values in the list before comparisons are done.

For example:

sorted(words, key=str.lower)
    # Will do a sort that ignores the case, since instead
    # of checking 'A' vs. 'b' it will check str.lower('A')
    # vs. str.lower('b').

sorted(intlist, key=abs)
    # Will sort a list of integers by magnitude, regardless
    # of whether they're negative or positive:
    # >>> sorted([-5,2,1,-8], key=abs)
    #     [1, 2, -5, -8]

The trick I used translated strings like this when doing the sorting:

"hello" => "bhello"  
"steve" => "asteve"

And so "steve" would come before "hello" in the comparisons, since the comparisons are done with the a/b prefix.

Note that this only affects the keys used for comparisons, not the data items that come out of the sort.

Answer from integer on Stack Overflow
🌐
W3Schools
w3schools.com β€Ί python β€Ί ref_list_sort.asp
Python List sort() Method
Remove List Duplicates Reverse ... Q&A Python Bootcamp Python Certificate Python Training ... The sort() method sorts the list ascending by default....
🌐
Python documentation
docs.python.org β€Ί 3 β€Ί howto β€Ί sorting.html
Sorting Techniques β€” Python 3.14.4 documentation
February 23, 2026 - Author, Andrew Dalke and Raymond Hettinger,. Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted lis...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί sort-a-list-in-python
Sort a list in python - GeeksforGeeks
July 23, 2025 - The sort() method sorts the list in place and modifies the original list.
🌐
Codecademy
codecademy.com β€Ί article β€Ί how-to-sort-a-list-in-python
How to sort a list in Python | Codecademy
Learn how to sort lists in Python using `.sort()` and `sorted()`. Explore custom sorting with keys, descending order sorting, and apply real-world sorting techniques efficiently.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-list-sort-method
Python List sort() Method - GeeksforGeeks
December 20, 2025 - The sort() method is used to arrange the elements of a list in a specific order, either ascending or descending. It changes the original list directly, so no new list is created.
🌐
Mimo
mimo.org β€Ί glossary β€Ί python β€Ί list-sort()
Python List sort(): Master Data Sorting | Learn Now
The Python sort() method organizes the elements of a list in ascending or descending order.
Find elsewhere
🌐
Programiz
programiz.com β€Ί python-programming β€Ί methods β€Ί list β€Ί sort
Python List sort()
Become a certified Python programmer. Try Programiz PRO! ... The list's sort() method sorts the elements of a list. ... # sort the list in ascending order prime_numbers.sort() print(prime_numbers) # Output: [2, 3, 5, 7, 11]
🌐
Google
developers.google.com β€Ί google for education β€Ί python β€Ί python sorting
Python Sorting | Python Education | Google for Developers
The easiest way to sort is with the sorted(list) function, which takes a list and returns a new list with those elements in sorted order.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί sort-in-python
sort() in Python - GeeksforGeeks
The sort() method in Python is used to arrange the elements of a list in a specific order.
Published Β  January 13, 2026
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί python-sort-list-how-to-order-by-descending-or-ascending
Python Sort List – How to Order By Descending or Ascending
September 3, 2021 - In this example, we have a list of numbers and we can use the sort() method to sort the list in ascending order.
🌐
ReqBin
reqbin.com β€Ί code β€Ί python β€Ί bk0xy1ql β€Ί python-sort-list-example
How do I sort a list in Python?
December 22, 2022 - For example, sort the list by odd and even elements or by the internal properties of an object in the list. In this Python Sorting a List example, we use the default list.sort() method.
🌐
W3Schools
w3schools.com β€Ί python β€Ί ref_func_sorted.asp
Python sorted() Function
Remove List Duplicates Reverse ... Python Bootcamp Python Certificate Python Training ... The sorted() function returns a sorted list of the specified iterable object....
Top answer
1 of 3
12
lst=[(11, "Jenny"),(8, "Adam"),(10, "Mark"),(8, "Ada")]
lst.sort(key=lambda x: (-x[0],len(x[1])) )
print (lst) # [(11, 'Jenny'), (10, 'Mark'), (8, 'Ada'), (8, 'Adam')]

The list method sort and the builtin function sorted accept a keyword argument key which is given a callable. Basically, for every element in the sequence, that element is passed to the key function and the return value of that function is actually what python uses to determine ordering when sorting. So, in the above, I use lambda to construct a function which returns a tuple from the input elements. The tuple is ordered first_element, lenth_of_second_element.

When tuples (or lists for that matter) are compared, it's much like comparing a string. You look at the first element, if they're the same, you continue on to look at the second element, then the third and so on until one element is greater than the other. e.g.

(1,2,3,4) > (1,2,3,3) #True

This ends up being handy for sorting in very interesting ways.

I suppose to round this out, I should mention that the algorithm that python uses to sort is stable. This means that if you sort by keyA and then you sort by keyB, two elements which compare equal based on keyB will maintain the order they had after the sort using keyA. In other words, a sort doesn't change the order of equal valued elements. So, the above could also be accomplished like this:

lst.sort(key=lambda x:len(x[1]))  #sort by length of names
lst.sort(key=lambda x:x[0], reversed=True) #sort by score (highest first instead of regular lowest first)

And I suppose no answer is complete without a link to something which explains it more elegantly. (Specifically, see the section on "Key Functions")

2 of 3
0

You'd have to iterate over each line and use the sort method on them

#open the file and read through the lines.
lines = open('yourfile.txt').readlines()

#iterate over each line, and split along the spaces 
pairs =[]
for line in lines:
   split_string = line.split('')
   num = int(split_string[0])
   pairs.append((num, split_string[1:])

#sort through the pairs (from mgilsons answer)
pairs.sort(key=lambda x: (x[0],len(x[1]))

edit: actually misread the question.

🌐
Real Python
realpython.com β€Ί python-sort
How to Use sorted() and .sort() in Python – Real Python
February 24, 2025 - The .sort() method sorts the list in place. You sort items in descending order by setting the reverse argument to True. The key argument accepts a function to customize the sort order.
🌐
Cisco
ipcisco.com β€Ί home β€Ί python list sort
Python List Sort | How To Sort Lists? | Sort & Reverse Methods ⋆
March 14, 2021 - We use python lists for various reasons. But sometimes these lists are mixed and we need to sort it. Here, we will learn Python List Sort. We use python sort () function to sort Python Lists alphabetically.
🌐
Python
docs.python.org β€Ί 3.10 β€Ί howto β€Ί sorting.html
Sorting HOW TO β€” Python 3.10.20 documentation
In this document, we explore the various techniques for sorting data using Python. A simple ascending sort is very easy: just call the sorted() function. It returns a new sorted list:
🌐
Python
docs.python.org β€Ί 3.9 β€Ί howto β€Ί sorting.html
Sorting HOW TO β€” Python 3.9.24 documentation
In this document, we explore the various techniques for sorting data using Python. A simple ascending sort is very easy: just call the sorted() function. It returns a new sorted list:
🌐
Python documentation
docs.python.org β€Ί 3 β€Ί tutorial β€Ί datastructures.html
5. Data Structures β€” Python 3.14.4 documentation
To loop over a sequence in sorted order, use the sorted() function which returns a new sorted list while leaving the source unaltered.