How can you sort data by size in Python?

To sort the size of the data, you can utilize either the built-in sorted() function or the list.sort() method.

  1. arranged in order
data = [5, 2, 8, 1, 9]
sorted_data = sorted(data)
print(sorted_data)

output:

[1, 2, 5, 8, 9]
  1. Sort the list.
data = [5, 2, 8, 1, 9]
data.sort()
print(data)

output:

[1, 2, 5, 8, 9]

Please note that the sorted() function will return a new sorted list, while the list.sort() method will sort the original list in place without returning a new list.

bannerAds