

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


list=[] for i in range (5): name = input(“Name: “) list.append(name.split()[-1]) list.sort() print(list)
last_names = [] # Using 'last_names' instead of 'list' to avoid overriding the built-in type for i in range(5): full_name = input("Name: ") # Collecting the full name from the user last_names.append(full_name.split()[-1]) # Extracting the last name and adding it to the list last_names.sort() # Sorting the list of last names alphabetically print(last_names) # Printing the sorted list
This approach ensures that regardless of how many parts a name has, it consistently extracts and sorts the last word of each input as the last name.