Would anyone please indicate to me why this is not working. I am trying to convert all instances of '02ND' to '2ND' for example where some of the input fields (f1) are already converted and some are not.

In codeblock:

 def calc(f1):
 list=['01ST', '02ND', '03RD', '04TH', '05TH', '06TH', '07TH', '08TH', '09TH']

    for i in list:

        if i in f1:
            f1 = f1.replace('0','')
            return f1.replace('0','')

            return f1

I simply call the function calc to populate a new field.

3 Answers 3


A bit explanation why the OP's function is not working:

f1 = a string

list= a list

When you are iterating over the list, for each member of list you will get either positive or negative response. So your list has nine members; you will get nine responses(+ and -). Then what the calc function will return, whole nine responses or one? Of-course python function will return only one in this case. Since only one return is allowed so calc function is returning the response for the last member i.e.'09TH' and you will get all instances of '09TH' with no leading "0" but nothing about all the other instances e.g.'01ST', '02ND', '03RD', '04TH', '05TH', '06TH', '07TH', '08TH'.


No need to iterate over the list, just check if f1 is in the list.

def calc(f1):
       list = ['01ST', '02ND', '03RD', '04TH', '05TH', '06TH', '07TH', '08TH', '09TH']  
       if f1 in list:
                  f1 = f1.replace('0','')
                  return f1
                  return f1

N.B. Avoid variable declaration with reserved words. You defined a list variable named list, list is reserved word of python.


You could just check if your field value is in the remove list:

def remove_zero(field):

    remove_list = ['01ST', '02ND', '03RD', '04TH', '05TH', '06TH', '07TH', '08TH', '09TH']

    if field in remove_list:
        field = field.replace('0','')
        return field
        return field

Then you'll want to do:


In this case, the same thing could be accomplished with !FieldName!.lstrip("0").

  • +1 Agree, this is the easiest solution with no codeblock necessary.
    – John
    Jul 27, 2015 at 13:18
  • Yes if all the field entry needs to be changed then this the best facile way to go.
    – Learner
    Aug 4, 2015 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.