Wednesday, 22 September 2021

Python: Sort list by multiple attributes

By supplying a custom function to the ‘key’ argument to the sort, sorted function, we can sort the data on multiple attributes.

 

Find the below examples.

 

my_emps.sort(key=lambda emp : (emp.age, emp.country))

Sort the employees list by age and country.

 

my_emps = sorted(my_emps, key=lambda emp : (emp.country, emp.name))

Sort the employees list by. country and name

 

Find the below working application.

 

sort_list_by_multiple_attributes.py

class Employee:
    def __init__(self, id, name, age, country):
        self.id = id
        self.name = name
        self.age = age
        self.country = country

    def __str__(self):
        return str(self.id)+"," +self.name + "," + str(self.age) + ","+self.country

def print_list(my_list, msg):
    print(msg)
    for item in my_list:
        print(item)
    print()

emp1 = Employee(1, "Krishna", 32, 'India')
emp2 = Employee(21, "PTR", 41, 'Canada')
emp3 = Employee(3, "Sankalp", 38, 'Canada')
emp4 = Employee(30, "Sankalp", 29, 'India')
emp5 = Employee(13, "Ramesh", 41, 'India')
emp6 = Employee(15, "Bala", 41, 'Canada')
emp7 = Employee(25, "Lakshman", 41, 'India')
emp8 = Employee(51, "Krishna", 33, 'Canada')

my_emps = [emp1, emp2, emp3, emp4, emp5, emp6, emp7, emp8]

print_list(my_emps, 'Actual list content')

my_emps.sort(key=lambda emp : (emp.age, emp.country))
print_list(my_emps, 'Sort by age and country')

my_emps = sorted(my_emps, key=lambda emp : (emp.country, emp.name))
print_list(my_emps, 'Sort by country and name')

 

Output

Actual list content
1,Krishna,32,India
21,PTR,41,Canada
3,Sankalp,38,Canada
30,Sankalp,29,India
13,Ramesh,41,India
15,Bala,41,Canada
25,Lakshman,41,India
51,Krishna,33,Canada

Sort by age and country
30,Sankalp,29,India
1,Krishna,32,India
51,Krishna,33,Canada
3,Sankalp,38,Canada
21,PTR,41,Canada
15,Bala,41,Canada
13,Ramesh,41,India
25,Lakshman,41,India

Sort by country and name
15,Bala,41,Canada
51,Krishna,33,Canada
21,PTR,41,Canada
3,Sankalp,38,Canada
1,Krishna,32,India
25,Lakshman,41,India
13,Ramesh,41,India
30,Sankalp,29,India

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment