Python Program to extracts elements from the list with digits in increasing order
Last Updated :
09 May, 2023
Given a 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 a string and then check if each digit is greater than the previous one using a loop.
Python3
test_list = [ 1234 , 7373 , 3643 , 3527 , 148 ]
print ( "The original list is : " + str (test_list))
res = []
for ele in test_list:
flag = True
for idx in range ( len ( str (ele)) - 1 ):
if str (ele)[idx + 1 ] < = str (ele)[idx]:
flag = False
if flag:
res.append(ele)
print ( "Extracted increasing digits : " + str (res))
|
Output:
The original list is : [1234, 7373, 3643, 3527, 148]
Extracted increasing digits : [1234, 148]
Time complexity: O(n*m) where n is the length of the list and m is the maximum number of digits in a number in the list.
Auxiliary space: O(k) where k is the number of increasing digit numbers in the list.
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 comparing it with original version. If they are same, the element is added to desired list.
Python3
test_list = [ 1234 , 7373 , 3643 , 3527 , 148 ]
print ( "The original list is : " + str (test_list))
res = [ele for ele in test_list if ''.join( sorted ( str (ele))) = = str (ele)]
print ( "Extracted increasing digits : " + str (res))
|
Output:
The original list is : [1234, 7373, 3643, 3527, 148]
Extracted increasing digits : [1234, 148]
Time complexity: O(nlogn), where n is the length of the list.
Auxiliary space: O(n), where n is the length of the list.
Method 3 : Using map() function and lambda
- Initialize the input list
- Define a lambda function to extract the increasing digits in each element of the list.
- Use the map() function along with the lambda function to extract the increasing digits in each element of the list.
- Print the result.
Python3
test_list = [ 1234 , 7373 , 3643 , 3527 , 148 ]
extract_inc_digits = lambda x: all ( str (x)[i] < str (x)[i + 1 ] for i in range ( len ( str (x)) - 1 ))
res = list ( filter (extract_inc_digits, test_list))
print ( "Extracted increasing digits : " + str (res))
|
Output
Extracted increasing digits : [1234, 148]
Time complexity: O(nk), where n is the length of the input list and k is the maximum number of digits in an element of the list.
Auxiliary space: O(k), since we are only storing the digits of each element in memory during the execution of the lambda function.
Method 4: Using the built-in filter() function
Steps:
- We define a function is_increasing() which takes a number as input and returns True if the digits of the number are in increasing order, and False otherwise.
- We use the filter() function to apply this function to each element of the test_list and return only the elements for which the function returns True.
- We convert the resulting filter object to a list using list() function.
- We print the final result.
Python3
test_list = [ 1234 , 7373 , 3643 , 3527 , 148 ]
print ( "The original list is : " + str (test_list))
def is_increasing(num):
digits = list ( str (num))
return all ( int (digits[i]) < int (digits[i + 1 ])
for i in range ( len (digits) - 1 ))
res = list ( filter (is_increasing, test_list))
print ( "Extracted increasing digits : " + str (res))
|
Output
The original list is : [1234, 7373, 3643, 3527, 148]
Extracted increasing digits : [1234, 148]
Time complexity: O(n * m), where n is the length of the test_list and m is the maximum number of digits in a number in the list.
Auxiliary space: O(m), where m is the maximum number of digits in a number in the list.
Method 5: Using recursion
Step-by-step approach:
- Define a function that takes a number as input.
- Convert the number to a string and check if its digits are in increasing order using a string manipulation technique.
- If the digits are not in increasing order, return False.
- If the number has only one digit or the digits are in increasing order, return True.
- Otherwise, recursively call the function on the number without its first digit.
- If the recursive call returns True and the first digit is less than the second digit, return True.
- Otherwise, return False.
- Initialize an empty list to store the result.
- Loop through each number in the test_list and call the function on it.
- If the function returns True, append the number to the result list.
- Return the result list.
Python3
test_list = [ 1234 , 7373 , 3643 , 3527 , 148 ]
print ( "The original list is : " + str (test_list))
def is_increasing(num):
str_num = str (num)
if len (str_num) = = 1 :
return True
elif str_num[ 0 ] > = str_num[ 1 ]:
return False
else :
return is_increasing( int (str_num[ 1 :]))
res = [num for num in test_list if is_increasing(num)]
print ( "Extracted increasing digits : " + str (res))
|
Output
The original list is : [1234, 7373, 3643, 3527, 148]
Extracted increasing digits : [1234, 148]
The time complexity of this method is O(n log n), where n is the length of the test_list.
The auxiliary space complexity of this method is O(log n), which is the maximum depth of the recursion.
Similar Reads
Python program to extract only the numbers from a list which have some specific digits
Given the elements List, extract numbers with specific digits. Input : test_list = [3456, 23, 128, 235, 982], dig_list = [2, 3, 5, 4] Output : [23, 235] Explanation : 2, 3 and 2, 3, 5 are in digit list, hence extracted elements.Input : test_list = [3456, 23, 28, 235, 982], dig_list = [2, 3, 5, 4, 8]
7 min read
Python Program to remove a specific digit from every element of the list
Given a list of elements, the task here is to write a Python program that can remove the presence of all a specific digit from every element and then return the resultant list. Examples: Input : test_list = [333, 893, 1948, 34, 2346], K = 3 Output : ['', 89, 1948, 4, 246] Explanation : All occurrenc
7 min read
Python Program to Split the Even and Odd elements into two different lists
In Python, it's a common task to separate even and odd numbers from a given list into two different lists. This problem can be solved using various methods. In this article, weâll explore the most efficient ways to split even and odd elements into two separate lists. Using List ComprehensionList com
3 min read
Python program to find N largest elements from a list
Given a list of integers, the task is to find N largest elements assuming size of list is greater than or equal o N. Examples : Input : [4, 5, 1, 2, 9] N = 2 Output : [9, 5] Input : [81, 52, 45, 10, 3, 2, 96] N = 3 Output : [81, 96, 52] A simple solution traverse the given list N times. In every tra
5 min read
Python program to extract characters in given range from a string list
Given a Strings List, extract characters in index range spanning entire Strings list. Input : test_list = ["geeksforgeeks", "is", "best", "for", "geeks"], strt, end = 14, 20 Output : sbest Explanation : Once concatenated, 14 - 20 range is extracted.Input : test_list = ["geeksforgeeks", "is", "best",
4 min read
Python - Extract elements with Frequency greater than K
Given a List, extract all elements whose frequency is greater than K. Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 3, 8], K = 3 Output : [4, 3] Explanation : Both elements occur 4 times. Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 6, 6], K = 2 Output : [4, 3, 6] Explanation : Occur 4, 3, and 3 time
7 min read
Python - Extracting Priority Elements in Tuple List
Sometimes, while working with Python Records, we can have a problem in which we need to perform extraction of all the priority elements from records, which usually occur as one of the binary element tuple. This kind of problem can have possible application in web development and gaming domains. Let'
5 min read
Python program to insert an element into sorted list
Inserting an element into a sorted list while maintaining the order is a common task in Python. It can be efficiently performed using built-in methods or custom logic. In this article, we will explore different approaches to achieve this. Using bisect.insort bisect module provides the insort functio
2 min read
Python Program to assign each list element value equal to its magnitude order
Given a list, the task is to write a Python program to assign each element value equal to its magnitude order. Input : test_list = [8, 3, 5, 8, 1, 5, 4] Output : [4, 1, 3, 4, 0, 3, 2] Explanation : 8 is 5th maximum hence 4 [starting from 0th index] is assigned to it. Input : test_list = [3, 2, 0] Ou
3 min read
Python program to find the Decreasing point in List
Given a list, get the index of element where the list shows the first negative trend, i.e first point where the next element < current element. If not found return -1. Input : test_list = [3, 6, 8, 9, 12, 5, 18, 1] Output : 4 Explanation : At 12 -> 5, first decreasing point occurs. Input : tes
4 min read