Open In App

Python – Remove Units from Value List

Sometimes, while working with Python value lists, we can have a problem in which we need to perform removal of units that come along with values. This problem can have direct application in day-day programming and data preprocessing. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [“54 cm”, “23 cm”, “12cm”, “19 cm”], unit = “cm”
 Output : [’54’, ’23’, ’12’, ’19’] 



Input : test_list = [“54 cm”], unit = “cm” 
Output : [’54’]

Method #1: Using regex() + list comprehension The combination of these functions can be used to solve this problem. In this, we approach by extracting just the numbers using regex, and remove rest of string units. 






# Python3 code to demonstrate working of
# Remove Units from Value List
# Using list comprehension + regex()
import re
 
# initializing list
test_list = ["54 kg", "23 kg", "12kg", "19  kg"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing unit
unit = "kg"
 
# Remove Units from Value List
# Using list comprehension + regex()
res = re.findall('\d+', ' '.join(test_list))
 
# printing result
print("List after unit removal : " + str(res))

Output : 
The original list is : ['54 kg', '23 kg', '12kg', '19  kg']
List after unit removal : ['54', '23', '12', '19']

Time complexity: O(n), where n is the length of the combined string of the list, because it has to traverse the string once to find all the digits using a regular expression. 
Auxiliary space: O(m), where m is the number of digits in the combined string, because it needs to store all the digits in a separate list.

Method #2: Using replace() + strip() + list comprehension The combination of above functions can be used to solve this problem. In this, we perform the task of replacing unit with empty string to remove it using replace() and stray spaces are handled using strip(). 




# Python3 code to demonstrate working of
# Remove Units from Value List
# Using replace() + strip() + list comprehension
import re
 
# initializing list
test_list = ["54 kg", "23 kg", "12kg", "19  kg"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing unit
unit = "kg"
 
# Remove Units from Value List
# Using replace() + strip() + list comprehension
res = [sub.replace(unit, "").strip() for sub in test_list]
 
# printing result
print("List after unit removal : " + str(res))

Output : 
The original list is : ['54 kg', '23 kg', '12kg', '19  kg']
List after unit removal : ['54', '23', '12', '19']

Time complexity: O(n), where n is the number of elements in the input list.
Auxiliary space: O(n), where n is the number of elements in the input list. This is because the list comprehension creates a new list with n elements. The space complexity of the other operations in the program is constant with respect to the input size.

Method #3 : Using split() and strip() methods




# Python3 code to demonstrate working of
# Remove Units from Value List
 
 
# initializing list
test_list = ["54 kg", "23 kg", "12kg", "19 kg"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing unit
unit = "kg"
 
# Remove Units from Value List
res=[]
for i in test_list:
    x=i.split(unit)[0]
    x=x.strip()
    res.append(x)
 
# printing result
print("List after unit removal : " + str(res))

Output
The original list is : ['54 kg', '23 kg', '12kg', '19 kg']
List after unit removal : ['54 ', '23 ', '12', '19 ']

Time Complexity : O(N)
Auxiliary Space : O(N)

Method 4: Using a loop and string methods:




test_list = ["54 kg", "23 kg", "12kg", "19  kg"]
unit = "kg"
 
res = []
for sub in test_list:
    if sub.endswith(unit):
        res.append(sub[:-len(unit)].strip())
    else:
        res.append(sub)
 
print("The original list is : " + str(test_list))
print("List after unit removal : " + str(res))

Output
The original list is : ['54 kg', '23 kg', '12kg', '19  kg']
List after unit removal : ['54', '23', '12', '19']

Time complexity: O(n), where n is the length of the input list test_list. The for loop iterates through each element in the list once, and the time complexity of each operation within the loop (i.e., endswith, append, strip, and len) is constant time, or O(1), so the total time complexity of the program is O(n).
Auxiliary space: O(n), since the output list res has the same length as the input list test_list. 

Method #5: Using map() and lambda function

This method uses the map() function and a lambda function to apply the replace() and strip() methods to each element in the list test_list.




test_list = ["54 kg", "23 kg", "12kg", "19 kg"]
unit = "kg"
res = list(map(lambda x: x.replace(unit, "").strip(), test_list))
print("List after unit removal : " + str(res))

Output
List after unit removal : ['54', '23', '12', '19']

Time complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n), because a new list is created to store the result.

Method #6: Using regular expression sub() method
We can use the sub() method from the re module to substitute the matching pattern (unit) with an empty string.




import re
 
# initializing list
test_list = ["54 kg", "23 kg", "12kg", "19  kg"]
 
# initializing unit
unit = "kg"
 
# Remove Units from Value List
# Using sub() method
res = [re.sub(unit, "", sub).strip() for sub in test_list]
 
# printing result
print("List after unit removal : " + str(res))

Output
List after unit removal : ['54', '23', '12', '19']

Time complexity: O(n) where n is the length of the list test_list. This is because we iterate through the list only once.
Auxiliary Space: O(n) because we create a new list of the same length as the original test_list.

Method #7: Using string slicing and isdigit() method

Step-by-step approach:




# Python3 code to demonstrate working of
# Remove Units from Value List
# Using string slicing and isdigit() method
 
# initializing list
test_list = ["54 kg", "23 kg", "12kg", "19  kg"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing unit
unit = "kg"
 
# Remove Units from Value List
# Using string slicing and isdigit() method
res = []
for item in test_list:
    temp_str = ""
    if item.endswith(unit):
        item = item[:-len(unit)]
    for char in item:
        if char.isdigit():
            temp_str += char
        else:
            res.append(temp_str)
            break
    else:
        res.append(temp_str)
 
# printing result
print("List after unit removal : " + str(res))

Output
The original list is : ['54 kg', '23 kg', '12kg', '19  kg']
List after unit removal : ['54', '23', '12', '19']

Time complexity: O(n*m), where n is the length of test_list and m is the average length of each item in test_list.
Auxiliary space: O(n), where n is the length of test_list.


Article Tags :