Taking a number and rearranging its digits in ascending order to form a new number in Python

In this Python tutorial, we will learn how to take a number and rearrange its digits in ascending order to form a new number.

We shall be sorting the number using the sort() function in Python.

The sort() function in Python sorts lists. ( It sorts lists in the ascending order by default.)

Code and Output: Rearrange digits to form a new number

Look at the following Python code and try to understand it.

Code in Python:-

#Initialising some numbers in a list
old_no = [321,431,598,965,79845,9685]
#Initialising an empty list to contain the new numbers
new_no = []
#Iterating over the list old_no
for num in old_no:
    #Initialising a counter to count the number of digits in num
    c = 0
    #Initialising a temporary variable to contain the value of num
    temp = num
    #Initialising an empty list to contain the digits of num
    x = []
    #Initialising the variable which will contain the new number
    no = 0
    #Counting the number of digits in num
    while temp != 0:
        temp = temp//10
        c = c+1
    #Extracting digits from num, putting them in a list an sorting the list
    for i in range(c):
        num = num//10
    #Forming the new digit after sorting the digit in ascending order
    for i in x:
        no = (no*10)+i
    #Appending the new number formed into the list new_no

print("The original numbers are:-")
for i in old_no:
print("The numbers after digit rearrangement are:-")
for i in new_no:


The original numbers are:-
The numbers after digit rearrangement are:-

Use a pen and paper if it helps and dry run this Python code by hand!

After you are done, read through the explanation given below!

 Explanation of the Python code

Initializing variables and starting the outer for loop:-

  • Initialize some numbers in a list old_no We will rearrange the digits of these numbers in ascending order and store it in another list called new_no. We shall initialize an empty list by this name in the next line.
  • Iterate over the list old_no using a for loop and hence for each iteration we have each element in the list old_no considered at the time in num. For example, in the first iteration, num will be 321, for the second iteration, num will be 431 and so on.
  • Initialize a counter ‘c‘ which will have the number of digits of num.
  • Put the value of num in a temporary variable called temp.
  • Initialize an empty list that will contain the digits of num. Here it is named x.
  • Initialize a new variable where we shall store the new number that is formed after rearranging the digits in ascending order. This variable is called ‘no’ and is set to 0.

Counting the digits:-

  • Run a while loop which will run while the value of temp is not equal to 0.
  • Floor divide num by 10.  Floor division is when we divide a number and truncate the decimal part that would remain.
  • Add 1 to c
  • Now that the while loop is over we have the number of digits that num contains which is stored in the variable c.

Digit extraction and appending to list:-

  • Run a for loop for c number of times
  • We use the Modulo Operator in Python which returns the remainder left when the operand on the left side of the operator is divided by the operand on it’s right. For example: 10%3 = 1
  • In the for loop, extract the digits of num using the Modulo operator and append it to the list x.
  • After the for loop mentioned above, use the sort() function in Python to sort the elements of x

Forming the new number:-

  • Iterate over the list x using a for loop and hence for each iteration, we have each element in the list x considered at the time in i. These elements in question are the digits of num
  • We add the digits from x to no in this loop using the statement no = (no*10)+i where i is a digit of num. Since no is 0 initially, we can add the first digit and then multiply by 10 to add the next digit and so on.
  • For example, let us consider the number 321. After digit extraction and sorting of the list x, the list stands as [1,2,3]. So in this loop, during the first iteration, no is (0*10) + 1 which is 1. In the second iteration, no is (1*10)+2 which is 12. In the third and final iteration, no is (12*10) +3 which gives us 123. This is our desired result.

Finishing the outer for loop and printing the lists:-

  • After this loop is complete,  append no to the list new_no.
  • Print the list old_no and the list new_no to show the contrast between the two lists and how the code has worked with regard to rearranging the digits of a number in an ascending order to form a new number.

Leave a Reply

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