Python List Sorting with sort and sorted

What is sort() and sorted() functions in Python?

Python provides two methods for sorting lists: sort() (which modifies the list in place) and sorted() (which returns a new sorted list). Both methods allow sorting in ascending or descending order.

Tutorials dojo strip

Why to use the sort and sorted functions?

Sorting data is a common requirement for organizing and presenting information. In scenarios like preparing barangay records or lists, sorting helps display information in an accessible way.

Syntax

  • list.sort() sorts the list in place.’
  • sorted(list) returns a sorted copy without modifying the original.

Example

Let’s say we want to sort the donations for the barangay using the sort and sorted functions.

Tutorials dojo strip