Sort Python List Using Multiple AttributesWe can sort a Python list in many ways. There are various algorithms made to sort a list. Sorting a 1D Python list is very straightforward. It requires the direct application of one of the available algorithms. However, given a nested Python list, on which basis will we sort the list? Let us see how to solve this problem. IntroductionSorting algorithms are commonly used in solving problems involving data structures. They can be applied to any data structure. We can use the in-built Python function to sort any Python list. The built0-in function sort() sorts the list in place while another function sorted() returns a sorted list. However, if we only use the function and pass the list as its parameter, we can sort it based on only one attribute. Therefore, if we wish to sort a nested Python list based on multiple attributes, we must look for other ways. The Ways to Sort a Python List Using Multiple AttributesSorting a list using multiple attributes has several benefits. This operation is the same as grouping the attributes based on the first column, sorting it, and then sorting the attributes of each column individually. Suppose we are given a nested Python list and have to sort it. If we sort based on the first element of the nested lists and two of the elements are identical, then the order of those two lists will be ambiguous. Then we must look for the second element, the third, and so on. Ways to sort the list
Method 1As discussed, Python lists can be sorted using the built-in function sorted(). However, the default nature of this function is to sort the list based on a single attribute. We can add one more parameter to the list to use this function to sort a list using multiple attributes. This parameter is called 'key'. The syntax of the function is as follows: Syntax: Firstly, we will define a nested Python list. The next step is to sort the nested list using the sorted() function. In the function, we will give the list as one of the parameters. The second parameter will be the key. The key is a function to sort the list. We will give a lambda function in the manner shown in the syntax. The first_column_index is the primary column to decide the sorting order. Each group will then be sorted according to the second_column_index. We can supply any number of columns we want. The function will return the sorted list. The last step is to print the sorted list. Let us see an example to understand the function of the working. We will create a nested Python list containing integers and strings. We will sort the list according to the multiple-column attributes. Code Output: [[2, 'Andrew', 'Actor', 30], [4, 'Harry', 'Singer', 29], [1, 'Harry', 'Singer', 32], [3, 'Tom', 'Actor', 28], [5, 'Tom', 'Director', 30]] As we can see, 'Tom' in column 1 is the same for two rows. According to the function, if two or more rows have values of column 1, then those rows will be sorted according to column 2. Therefore, 'Actor' comes before 'Director'. Now, the rows at index 0 and 3 have identical values for the second column indices. Therefore, these rows are sorted according to the third-column index. Time complexity: The function's time complexity is O(log n). However, we are sorting the whole list multiple times; therefore, the time complexity of sorting a nested list is O(n * log n). Space complexity: We are storing the sorted list in a different variable; hence the space complexity will be O(n) Approach - 2In this approach, we will use the itemgetter function. We will use the function with the sorted function again. This function will allow us to sort a list using as many attributes as possible. The function works similarly to the lambda function; this time, we need to give the function a pointer pointing toward the item. Let us see the syntax of the itemgetter To use the itemgetter function, we need to import the operator library. After importing the library, the next step is to create a nested list. We will sort this nested list according to 3 columns. We will use the sorted function to give the list name and again use the key parameter. To this parameter, we will give the itemgetter function along with the column indices as the lambda function. Finally, we will print the sorted list. We will take the same nested list we used in the first example. We will sort the list according to the first, second, and third column index. As we can see, 'Tom' in column 1 is the same for two rows. According to the function, if two or more rows have values of column 1, then those rows will be sorted according to column 2. Therefore, 'Actor' comes before 'Director'. Now, the rows at index 0 and 3 have identical values for the second column indices. Therefore, these rows are sorted according to the third-column index. Code Output: [[2, 'Andrew', 'Actor', 30], [4, 'Harry', 'Singer', 29], [1, 'Harry', 'Singer', 32], [3, 'Tom', 'Actor', 28], [5, 'Tom', 'Director', 30]] Time complexity: The sorting function must sort the list multiple times. Sorting the list single time will have a time complexity of O(log n). However, sorting the lost n times will make the time complexity O(n log n), where n is the number of elements of the list. Space complexity: We have used space to store the sorted list. Since the original list took space O(n), the sorted list will also take space O(n). Approach - 3In this approach, we have used the attrgetter() function to sort the list based on multiple attributes. We must import the operator module to use the attrgetter() method. Also, we cannot give column indices to this method; therefore, we first need to give names to the columns of the nested list. Code Output: [[2, 'Andrew', 'Actor', 30], [4, 'Harry', 'Singer', 29], [1, 'Harry', 'Singer', 32], [3, 'Tom', 'Actor', 28], [5, 'Tom', 'Director', 30]] Next TopicAlteryx vs Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India