173

I have several alphanumeric strings like these

listOfNum = ['000231512-n','1209123100000-n00000','alphanumeric0000', '000alphanumeric']

The desired output for removing trailing zeros would be:

listOfNum = ['000231512-n','1209123100000-n','alphanumeric', '000alphanumeric']

The desired output for leading trailing zeros would be:

listOfNum = ['231512-n','1209123100000-n00000','alphanumeric0000', 'alphanumeric']

The desire output for removing both leading and trailing zeros would be:

listOfNum = ['231512-n','1209123100000-n', 'alphanumeric', 'alphanumeric']

For now i've been doing it the following way, please suggest a better way if there is:

listOfNum = ['000231512-n','1209123100000-n00000','alphanumeric0000', \
'000alphanumeric']
trailingremoved = []
leadingremoved = []
bothremoved = []

# Remove trailing
for i in listOfNum:
  while i[-1] == "0":
    i = i[:-1]
  trailingremoved.append(i)

# Remove leading
for i in listOfNum:
  while i[0] == "0":
    i = i[1:]
  leadingremoved.append(i)

# Remove both
for i in listOfNum:
  while i[0] == "0":
    i = i[1:]
  while i[-1] == "0":
    i = i[:-1]
  bothremoved.append(i)
alvas
  • 115,346
  • 109
  • 446
  • 738

7 Answers7

360

What about a basic

your_string.strip("0")

to remove both trailing and leading zeros ? If you're only interested in removing trailing zeros, use .rstrip instead (and .lstrip for only the leading ones).

More info in the doc.

You could use some list comprehension to get the sequences you want like so:

trailing_removed = [s.rstrip("0") for s in listOfNum]
leading_removed = [s.lstrip("0") for s in listOfNum]
both_removed = [s.strip("0") for s in listOfNum]
Pierre GM
  • 19,809
  • 3
  • 56
  • 67
  • 4
    Is there any clever adjustment for this answer for the special case of `s = '0'`? – Charles Oct 16 '17 at 16:19
  • 19
    @Charles : Yes! I just had the same problem, and you can do `s.strip("0") or "0"`: if your string turns into the empty string, it will evaluate as `False` by or and will be replaced by the desired string `"0"` – tarulen May 31 '18 at 12:14
  • @Pierre GM Thank you . It really helps and it is very simple. Upvoted it – Vishav Gupta Apr 27 '20 at 13:55
21

Remove leading + trailing '0':

list = [i.strip('0') for i in list_of_num]

Remove leading '0':

list = [i.lstrip('0') for i in list_of_num]

Remove trailing '0':

list = [i.rstrip('0') for i in list_of_num]
alper
  • 2,919
  • 9
  • 53
  • 102
fho
  • 470
  • 5
  • 12
9

You can simply do this with a bool:

if int(number) == float(number):   
    number = int(number)   
else:   
    number = float(number)
alper
  • 2,919
  • 9
  • 53
  • 102
Bjarne
  • 99
  • 1
  • 1
4

Did you try with strip() :

listOfNum = ['231512-n','1209123100000-n00000','alphanumeric0000', 'alphanumeric']
print [item.strip('0') for item in listOfNum]

>>> ['231512-n', '1209123100000-n', 'alphanumeric', 'alphanumeric']
Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
3

Assuming you have other data types (and not only string) in your list try this. This removes trailing and leading zeros from strings and leaves other data types untouched. This also handles the special case s = '0'

e.g

a = ['001', '200', 'akdl00', 200, 100, '0']

b = [(lambda x: x.strip('0') if isinstance(x,str) and len(x) != 1 else x)(x) for x in a]

b
>>>['1', '2', 'akdl', 200, 100, '0']

Kweweli
  • 327
  • 3
  • 7
3

pandas also propose a convenient method :

listOfNum = pd.Series(['000231512-n','1209123100000-n00000','alphanumeric0000', '000alphanumeric'])

listOfNum.str.strip("0")
listOfNum.str.ltrip("0")
listOfNum.str.rtrip("0")

The first one would give, for instance :

0           231512-n
1    1209123100000-n
2       alphanumeric
3       alphanumeric
dtype: object

This might be more convenient when working with DataFrames

linog
  • 5,786
  • 3
  • 14
  • 28
1

str.strip is the best approach for this situation, but more_itertools.strip is also a general solution that strips both leading and trailing elements from an iterable:

Code

import more_itertools as mit


iterables = ["231512-n\n","  12091231000-n00000","alphanum0000", "00alphanum"]
pred = lambda x: x in {"0", "\n", " "}
list("".join(mit.strip(i, pred)) for i in iterables)
# ['231512-n', '12091231000-n', 'alphanum', 'alphanum']

Details

Notice, here we strip both leading and trailing "0"s among other elements that satisfy a predicate. This tool is not limited to strings.

See also docs for more examples of

more_itertools is a third-party library installable via > pip install more_itertools.

pylang
  • 40,867
  • 14
  • 129
  • 121