I'm looking for some help in explaining what exactly is occurring with the following code:

MaterialLife = "LifeMaterial(!"+Material+"!)"
codeblock2 = """def LifeMaterial(mat):
    material = {"ABS":90,"AC":100,"BRICK":100,"CI":70,"CLAY":100,
          "CSP":70,"CSPA":90, "CSPH":70,
          " ":90,"UNK":90,None:90}

    life = material [mat]
    return life"""

arcpy.CalculateField_management(ins_pipe, MaterialLifetime, MaterialLife, "PYTHON_9.3", codeblock2)

I know the basic structure in that there's an expression (MaterialLife) and a codeblock (codeblock2) that are used to perform a calculate field function. The input field to the expression (Material or mat in the codeblock) contains all of the different types of materials that are in the codeblock ("CONC", "CONPP" etc.). The numbers beside each of the materials are ages that are to be assigned to each material. The end result of the function is that all of the ages associated to the material types are populated in the field "MaterialLifetime" based on its corresponding Material type.

My main questions are:

  • How does the program know to properly go through every entry in the table even though they are not listed in the order that is laid out in the codeblock without any if statements?
  • How does the program know to populate the "MaterialLifetime" field with ONLY the age and not the material type as well?
  • As per the Tour there should be only one question asked per question. – PolyGeo Feb 28 '19 at 20:02

That is a dictionary, not a list. You provide a key, for example "ABS" like this: material["ABS"] and you get the value 90 returned. Very useful instead of many if statements.

The key is in one of your fields and the return value is used to populate another.

(I recommend you use the da.UpdateCursor instead of Field Calculator. More versatile and easier to use)

| improve this answer | |

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