Python - Remove Units from Value List
Last Updated :
16 Mar, 2023
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
# 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
# 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
# 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))
OutputThe 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:
Python3
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))
OutputThe 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.
Python3
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))
OutputList 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.
Python3
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))
OutputList 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:
- Initialize an empty list called res.
- Iterate through each item in test_list using a for loop.
- Initialize an empty string called temp_str.
- For each item, remove the last len(unit) characters if the last characters in the item match the unit. This can be done by string slicing.
- Iterate through each character in the resulting string using another for loop.
- If the character is a digit, add it to temp_str.
- If the character is not a digit, append temp_str to res and break out of the inner loop.
- If the end of the string is reached, append temp_str to res.
- Return res.
Python3
# 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))
OutputThe 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.
Similar Reads
Python - Remove suffix from string list To remove a suffix from a list of strings, we identify and exclude elements that end with the specified suffix. This involves checking each string in the list and ensuring it doesn't have the unwanted suffix at the end, resulting in a list with only the desired elements.Using list comprehensionUsing
3 min read
Python - Remove List Item Removing List Item can be achieved using several built-in methods that provide flexibility in how you remove list items. In this article, we'll explore the different ways to remove list items in Python.Removing Item by Value with remove()The remove() method allows us to remove the first occurrence o
3 min read
Python - Remove digits from Dictionary String Values List We are given dictionary we need to remove digits from the dictionary string value list. For example, we are given a dictionary that contains strings values d = {'key1': ['abc1', 'def2', 'ghi3'], 'key2': ['xyz4', 'lmn5']} we need to remove digits from dictionary so that resultant output becomes {'key
3 min read
Python | Remove given element from the list Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
7 min read
Python | Removing strings from tuple Sometimes we can come across the issue in which we receive data in form of tuple and we just want the numbers from it and wish to erase all the strings from them. This has a useful utility in Web-Development and Machine Learning as well. Let's discuss certain ways in which this particular task can b
4 min read