pythonpython-3.xlist-manipulation

Removing a Specific Char From a List


What I want to know is if there is a built-in-function with the Python Library that would remove a specific character from a list?

EG:

In this case I want to remove all of the zeroes from the list, how would I do that.

[76499618373903541704700923371133789862262100000000000000000000000]

Note: This list has only 1 value


Solution

  • You have to divide by 10, as long as the number is divisible by 10:

    def remove_zeros(number):
        while number and number % 10 == 0:
            number //= 10
        return number
    
    number_in_a_list = [764996183739035417047009233711337898622621000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]
    
    result = [remove_zeros(n) for n in number_in_a_list]