How can you sort a Python list in ascending order?

You can sort Python list data in ascending order by using the sort() method of the list or by using the built-in function sorted().

Use the sort() method:

my_list = [4, 2, 1, 5, 3]
my_list.sort()
print(my_list)

Use the sorted() function:

my_list = [4, 2, 1, 5, 3]
sorted_list = sorted(my_list)
print(sorted_list)

Both methods will sort the elements in the list in ascending order.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds