DeveloperBreeze

Sort List of Objects by Attribute in Python

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

# Create instances of Person
people = [Person('Alice', 30), Person('Bob', 25), Person('Charlie', 35)]

# Sort people by age using a lambda function as the key
sorted_people = sorted(people, key=lambda x: x.age)

Continue Reading

Handpicked posts just for you — based on your current read.

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!