# Python Program to Print Number in Ascending Order which contains 1, 2 and 3 in their Digits

In the previous article, we have discussed Python Program to Check if Array can be Sorted with One Swap
Given a list and the task is to print the numbers with the digits 1, 2, and 3 in ascending order, which are separated by commas.

Examples:

Example1:

Input:

Given List = [67123, 1234, 985, 126, 1011]

Output:

The numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas=  [1234, 67123]

Example2:

Input:

Given List = [75, 4123, 87123, 5312, 9098]

Output:

The numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas= [4123, 5312, 87123]

## Program to Print Number in Ascending Order which contains 1, 2, and 3 in their Digits in Python

Below are the ways to print the numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas.

### Method #1: Using For Loop (Static Input)

Approach:

• Give the list as static input and store it in a variable.
• Take a new empty list say “numb” and store it in another variable.
• Loop in the given list using the for loop.
• Inside the loop, convert the iterator value to the string using the str() function and store it in another variable.
• Check if 1 and 2 and 3 are present in the string number using the if conditional statement and ‘and’ keyword.
• If the statement is true, then append the iterator value to the above initialized new empty list “numb”.
• Sort the above list “numb” using the sort() function.
• Print the list “numb” to print the numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas.
• The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gven_lst = [67123, 1234, 985, 126, 1011]
# Take a new empty list say "numb" and store it in another variable.
numb = []
# Loop in the given list using the for loop.
for itr in gven_lst:
# Inside the loop, convert the iterator value to the string using the str() function
# and store it in another variable.
strng_number = str(itr)
# Check if 1 and 2 and 3 are present in the string number using the if conditional
# statement and 'and' keyword.
if '1' in strng_number and '2' in strng_number and '3' in strng_number:
# If the statement is true, then append the iterator value to the above initialized
# new empty list "numb".
numb.append(itr)
# Sort the above list "numb" using the sort() function.
numb.sort()
# print the list "numb" to print the numbers with the digits 1, 2, and 3 in ascending order,
# which is separated by commas.
print("The numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas= ", numb)


Output:

The numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas=  [1234, 67123]

### Method #2: Using For loop (User Input)

Approach:

• Give the list as user input using list(),map(),input(),and split() functions.
• Store it in a variable.
• Take a new empty list say “numb” and store it in another variable.
• Loop in the given list using the for loop.
• Inside the loop, convert the iterator value to the string using the str() function and store it in another variable.
• Check if 1 and 2 and 3 are present in the string number using the if conditional statement and ‘and’ keyword.
• If the statement is true, then append the iterator value to the above initialized new empty list “numb”.
• Sort the above list “numb” using the sort() function.
• Print the list “numb” to print the numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas.
• The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gven_lst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Take a new empty list say "numb" and store it in another variable.
numb = []
# Loop in the given list using the for loop.
for itr in gven_lst:
# Inside the loop, convert the iterator value to the string using the str() function
# and store it in another variable.
strng_number = str(itr)
# Check if 1 and 2 and 3 are present in the string number using the if conditional
# statement and 'and' keyword.
if '1' in strng_number and '2' in strng_number and '3' in strng_number:
# If the statement is true, then append the iterator value to the above initialized
# new empty list "numb".
numb.append(itr)
# Sort the above list "numb" using the sort() function.
numb.sort()
# print the list "numb" to print the numbers with the digits 1, 2, and 3 in ascending order,
# which is separated by commas.
print("The numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas= ", numb)


Output:

Enter some random List Elements separated by spaces = 75 4123 87123 5312 9098
The numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas= [4123, 5312, 87123]

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.