Question:

Write a program to sort the (name, age, height) tuples by ascending order where name is string, age and height are numbers.

1: Sort based on name;

2: Then sort based on age;

3: Then sort by score.

The priority is that name > age > score.

We use itemgetter to enable multiple sort keys.

Program:

Explanation:

operator is a built-in module providing a set of convenient operators. In two words operator.itemgetter(n) constructs a callable that assumes an iterable object (e.g. list, tuple, set) as input, and fetches the n-th element out of it.

Output:

Leave a Reply