Home > front end >  How to sort a list in Python alphabetically by the index of the original list
How to sort a list in Python alphabetically by the index of the original list

Time:04-10

I have a list: list_original= ['Zoe', 'Alex', 'Jennifer', 'Mike', 'Robert']

How can I iterate through the indices of list_original in order to get the names alphabatically? --> ['Alex', 'Jennifer', 'Mike', 'Robert', 'Zoe']

Therefore I wanted to code a for-loop.

list_alphabetical = []

for i in ... : list_alphabetical.append(i)

Could someone help? :)

CodePudding user response:

Python has a built in method called sorted which can help you. See the Python docs for more details.

list_original= ['Zoe', 'Alex', 'Jennifer', 'Mike', 'Robert']
list_sorted = sorted(list_original)
print(list_sorted)
>>> ['Alex', 'Jennifer', 'Mike', 'Robert', 'Zoe']

CodePudding user response:

list_original= ['Zoe', 'Alex', 'Jennifer', 'Mike', 'Robert']
list_alphabetical = sorted(list_original)
  • Related