Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython – Remove leading 0 from Strings List

Python – Remove leading 0 from Strings List

Sometimes, while working with Python, we can have a problem in which we have data which we need to perform processing and then pass the data forward. One way to process is to remove a stray 0 that may get attached to a string while data transfer. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using lstrip() + list comprehension 

This is one of the one-liners with the help of which this task can be performed. In this, we strip the leading 0 using lstrip and the extension of logic to list is done using list comprehension. 

Step-by-step approach:

  • Initialize a list of strings.
  • Print the original list of strings.
  • Use a list comprehension to loop through each element in the list.
  • For each element, use the lstrip() method to remove any leading 0s.
  • Append the modified string to a new list.
  • Print the new list of strings with leading 0s removed.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate
# Remove leading 0 from Strings List
# using lstrip() + list comprehension
 
# Initializing list
test_list = ['012', '03', '044', '09']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove leading 0 from Strings List
# using lstrip() + list comprehension
res = [ele.lstrip('0') for ele in test_list]
 
# printing result
print("The string list after leading 0 removal : " + str(res))


Output

The original list is : ['012', '03', '044', '09']
The string list after leading 0 removal : ['12', '3', '44', '9']

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

Method #2: Using startswith() + loop + list slicing 

This is one of the ways in which this task can be performed. In this, we check for the initial 0 using startswith(), and then list slicing is used to remake the string excluding 0. 

Python3




# Python3 code to demonstrate
# Remove leading 0 from Strings List
# using startswith() + loop + list slicing
 
# Initializing list
test_list = ['012', '03', '044', '09']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove leading 0 from Strings List
# using startswith() + loop + list slicing
for idx in range(len(test_list)):
    if test_list[idx].startswith('0'):
        test_list[idx] = test_list[idx][1:]
 
# printing result
print("The string list after leading 0 removal : " + str(test_list))


Output

The original list is : ['012', '03', '044', '09']
The string list after leading 0 removal : ['12', '3', '44', '9']

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

Method #3 : Using find() and slicing methods

Python3




# Python3 code to demonstrate
# Remove leading 0 from Strings List
 
# Initializing list
test_list = ['012', '03', '044', '09']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove leading 0 from Strings List
 
for i in range(len(test_list)):
    if test_list[i].find('0') == 0:
        test_list[i] = test_list[i][1:]
 
# printing result
print("The string list after leading 0 removal : " + str(test_list))


Output

The original list is : ['012', '03', '044', '09']
The string list after leading 0 removal : ['12', '3', '44', '9']

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

Method #4 : Using int() function

Python3




# Python3 code to demonstrate
# Remove leading 0 from Strings List
 
# Initializing list
test_list = ['012', '03', '044', '09']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove leading 0 from Strings List
 
for i in range(len(test_list)):
    test_list[i] = int(test_list[i])
 
# printing result
print("The string list after leading 0 removal : " + str(test_list))


Output

The original list is : ['012', '03', '044', '09']
The string list after leading 0 removal : [12, 3, 44, 9]

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

Method #5: Using regex Expressions

One more approach to solving this problem could be using Regular Expressions (re module). We can use the re.sub() function to search for the pattern ‘^0+’ (i.e., one or more zeros at the start of the string) and replace it with an empty string.

Python3




# Python3 code to demonstrate
# Remove leading 0 from Strings List
# using re.sub()
 
import re
 
# Initializing list
test_list = ['012', '03', '044', '09']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove leading 0 from Strings List
# using re.sub()
 
res = [re.sub("^0+", "", ele) for ele in test_list]
 
# printing result
print("The string list after leading 0 removal : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is : ['012', '03', '044', '09']
The string list after leading 0 removal : ['12', '3', '44', '9']

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

Method #6: Using map() function

You can use the map() function to apply the int() function to each element of the list in a more concise way than using a for loop. Here’s how to use map() to remove leading zeros from strings in a list:

  1. Initialize a list test_list with elements as strings containing leading zeros.
  2. Print the original list using print() function.
  3. Use the map() function to apply the int() function to each element of the list in a concise way using a lambda function.
  4. Convert the output of map() function to list using list() function and store it in test_list.
  5. Print the final list with leading zeros removed using print() function.

Python3




# Initializing list
test_list = ['012', '03', '044', '09']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove leading 0 from Strings List using map() function
test_list = list(map(lambda x: int(x), test_list))
 
# printing result
print("The string list after leading 0 removal : " + str(test_list))


Output

The original list is : ['012', '03', '044', '09']
The string list after leading 0 removal : [12, 3, 44, 9]

Time complexity: O(n) where n is the number of elements in the list.
Auxiliary space: O(n) because we create a new list with the same number of elements as the original list.

RELATED ARTICLES

Most Popular

Recent Comments