Sort List in Python

Sort a list of numbers in ascending order.

BeginnerList ProgramsExample 9 of 25
sort-list.py
1# Program to sort a list of numbers
2
3numbers = list(map(float, input("Enter numbers separated by space: ").split()))
4
5numbers.sort()
6
7print("Sorted list:", numbers)

Output

Enter numbers separated by space: 3 1 4 2
Sorted list: [1.0, 2.0, 3.0, 4.0]

What's going on

We use the in-place .sort() method to sort the list in ascending order.

After this program

If the idea is still fuzzy, read Day 5: Lists and List Methods in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 list programs, or see every topic on Python exercises.