Python sort list of numbers ascending – Python : Sort a List of Numbers in Ascending or Descending Order | list.sort() vs sorted()

Python sort list of numbers ascending: A list is used in Python to store the sequence of different types of data. Python lists are mutable, which means that their elements can be changed after they have been created. Python, on the other hand, has six data types that can be used to store sequences, with the first two on the list being the most common and accurate.

A list is a collection of various types of values or objects. The list items are separated by a comma (,), which is enclosed in square brackets [].

Example:

Input:

givenlist = [3, 5, 1, 6, 9, 2, 8, 7]

Output:

Ascending order:

[1, 2, 3, 5, 6, 7, 8, 9]

Descending order:

[9, 8, 7, 6, 5, 3, 2, 1]

Sort the given list

Sorting numbers in python: In this article, we’ll look at two methods for sorting a list of numbers in ascending and descending order.

Method #1: Using sort() function to sort in ascending order:

It’s a built-in function that takes an iterable and returns a new sorted list from it.
Note:

  •  sort is a list class method that can only be used with lists. It is not a built-in that has been passed an iterable.
  • sort() returns None and modifies the existing values. Let’s look at the effects of both of these code differences.

Let us the sort the list of integers in ascending order

Below is the implementation:

# Driver code
# Given list
givenlist = [3, 5, 1, 6, 9, 2, 8, 7]
# sorting the given list in ascending order
givenlist.sort()
print(givenlist)

Output:

[1, 2, 3, 5, 6, 7, 8, 9]

Method #2: Using sort() function to sort in descending order:

Using reverse=True gives the list in descending order

Below is the implementation:

# Driver code
# Given list
givenlist = [3, 5, 1, 6, 9, 2, 8, 7]
# sorting the given list in descending order
givenlist.sort(reverse=True)
print(givenlist)

Output:

[9, 8, 7, 6, 5, 3, 2, 1]

Method #3: Using sorted() function to sort in ascending order

The sorted() function returns a sorted list of the iterable object specified. You can specify whether the order should be ascending or descending. Numbers are sorted numerically, while strings are sorted alphabetically. It is important to note that you cannot sort a list that contains BOTH string and numeric values.

Characteristics of sorted() function:

  • It was not necessary to define the function sorted(). It is a built-in function that is available in a standard Python installation.
  • With no additional arguments or parameters, sorted() orders the values in numbers in ascending order, from smallest to largest.
  • Because sorted() returns sorted results and does not change the original value in place, the original numbers variable remains unchanged.
  • When sorted() is invoked, it returns an ordered list as a result.

Below is the implemenation:

# Driver code
# Given list
givenlist = [3, 5, 1, 6, 9, 2, 8, 7]

# using sorted function to sort the given list in ascending order
newlist = sorted(givenlist)
# printing the newlist
print(newlist)

Output:

[1, 2, 3, 5, 6, 7, 8, 9]

Method #4: Using sorted() function to sort in descending order:

Using reverse=True gives the list in descending order

Below is the implementation:

# Driver code
# Given list
givenlist = [3, 5, 1, 6, 9, 2, 8, 7]

# using sorted function to sort the given list in descending order using reverse=True
newlist = sorted(givenlist, reverse=True)
# printing the newlist
print(newlist)

Output:

[9, 8, 7, 6, 5, 3, 2, 1]

 

Related Programs: