Sort a list in alphabetical order in Python

In python, sorted function can be used to sort a list in alphabetical order.

Syntax
 sorted(iterable, key=None, reverse=False)

Note : sorted function returns a new list containing all items from the iterable in ascending order.

Example 1:
cars = ["Mercedes Benz","Honda","Audi", "Volkswagen","Audi"]
sorted(cars)


Output
['Audi', 'Audi', 'Honda', 'Mercedes Benz', 'Volkswagen']


Example 2: Sort in reverse alphabetical order
cars = ["Mercedes Benz","Honda","Audi", "Volkswagen","Audi"]
sorted(cars, reverse=True)


Output
['Volkswagen', 'Mercedes Benz', 'Honda', 'Audi', 'Audi']


Example 3: Sort list of objects using key function
def nameFunc(x):
    return x["name"]

students = [{"name":"Chris","Age": 20},{"name":"Adam", "Age": 25}]

sorted(students,key=nameFunc)


Output
[{'Age': 25, 'name': 'Adam'}, {'Age': 20, 'name': 'Chris'}]