Python Program to extracts elements from a list with digits in increasing order


When it is required to extracts elements from a list with digits in increasing order, a simple iteration, a flag value and the ‘str’ method is used.

Below is a demonstration of the same −

Example

 Live Demo

my_list = [4578, 7327, 113, 3467, 1858]

print("The list is :")
print(my_list)

my_result = []

for element in my_list:
   my_flag = True
   for index in range(len(str(element)) - 1):

      if str(element)[index + 1] <= str(element)[index]:
         my_flag = False

   if my_flag:
      my_result.append(element)

print("The result is :")
print(my_result)

Output

The list is :
[4578, 7327, 113, 3467, 1858]
The result is :
[4578, 3467]

Explanation

  • A list is defined and displayed on the console.

  • An empty list is defined.

  • The list is iterated over, and flag is set to Boolean ’True’.

  • Every element is first converted to list, and compared with its consecutive element.

  • If second element is less than or equal to first element, the flag value is set to Boolean ‘False’.

  • If the Boolean flag is ‘True’ in the end, the element is appended to the empty list.

  • This is the output that is displayed on the console.

Updated on: 06-Sep-2021

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements