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

Given List of elements, extract all elements which have digits that are increasing in order.

Input : test_list = [1234, 7373, 3643, 3527, 148, 49] 
Output : [1234, 148, 49] 
Explanation : All elements have increasing digits.
Input : test_list = [12341, 7373, 3643, 3527, 1481, 491] 
Output : [] 
Explanation : No elements have all increasing digits. 

Method 1 : Using loop and str()

In this, we convert each element to string and then check if their each digit is greater than previous one using loop.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# initializing list
test_list = [1234, 7373, 3643, 3527, 148]
  
# printing original list
print("The original list is : " + str(test_list))
  
# loop to check for each element
res = []
for ele in test_list:
    flag = True
      
    for idx in range(len(str(ele)) - 1):
          
        # checking for each next digit
        if str(ele)[idx + 1] <= str(ele)[idx]:
            flag = False
      
    if flag:
        res.append(ele)
          
# printing result 
print("Extracted increasing digits : " + str(res))

chevron_right


Output:



The original list is : [1234, 7373, 3643, 3527, 148]
Extracted increasing digits : [1234, 148]

Method 2 : Using sorted(), list comprehension and str()

In this, we test for each digit of an element to be increasing by sorting each element and compare it with original version. If they are same, the element is added to desired list.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# initializing list
test_list = [1234, 7373, 3643, 3527, 148]
  
# printing original list
print("The original list is : " + str(test_list))
  
# sorting and comparing for equality
res = [ele for ele in test_list if ''.join(sorted(str(ele))) == str(ele)]
          
# printing result 
print("Extracted increasing digits : " + str(res))

chevron_right


Output:

The original list is : [1234, 7373, 3643, 3527, 148]
Extracted increasing digits : [1234, 148]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.