I need help with a python code section i don't understand.
students = [
("Alice", 22),
("Bob", 20),
("Charlie", 25),
("David", 21)
]
sorted_students = sorted(students, key=lambda x: x[1])
print(sorted_students)
# Output: [('Bob', 20), ('David', 21), ('Alice', 22), ('Charlie', 25)]
Why the hell does key=lambda x: x[1]
sorts things? As far as i know, all this is doing is taking an iterable x and returning the first index of it with x[1], in that list it would be 'David',21. I just don't get why it sorts and why it takes the second element of each tuple.
Could some anon explain it in detail?