How to sort a list alphabetically by the second word in a string

list, python, sorting

Solution

linesList.sort(key=lambda s: s.split()[1])

More info: https://wiki.python.org/moin/HowTo/Sorting#Key_Functions

Problem

If I have a list and I want to keep adding lines to it and sorting them alphabetically by their last name, how could this be done? Sorted only seems to rearrange them by the first letter of the string. ``` line = "James Edward" #Example line linesList.append("".join(line)) #Add it to a list linesList = sorted(linesList) #Sort alphabetically ```

Original source