# arcpy.da.UpdateCursor - how to make multiplication of each column with the selected column, for each row in particular?

If I have a table with a number of columns [col1, col2, col3 ... col_n] how to perform multiplication of each column with one selected column, for example:

``````multiplication = col1 * col_def, col2 * col_def, col3 * col_def ... col_n * col_def
``````

and then to determine the sum, for example:

``````sum_multiplication = Sum (multiplication)
``````

I try as follows, but does not go:

``````with arcpy.da.UpdateCursor(fc, (fieldNameList)) as cursor:

for row in cursor:

for i in range(len(fieldNameList)):
hi = sum(row[i] * row)
row = hi
cursor.updateRow(row)
``````

Does anyone have an idea how to solve this problem?

• I'm not quite sure what you're trying to do here, but why are you passing in fieldNameList as a tuple to UpdateCursor? Presumably it's already a list or tuple. You don't need the parantheses. Also not sure what is supposed to be in the multiplication variable. That looks incorrect to me, but maybe it's a method I don't know. I'll post my take on what I think you want as an answer. Also also, you're calling Sum() uppercase, when the Python method is sum() lowercase. If you have another function named Sum(), this is not a good idea, even though Python's case sensitivity should be ok with it. – recurvata Mar 6 '15 at 23:55
• I want to by option iteration perform arithmetic functions. I understand to Sum this was just an example. fieldNameList ---> gis.stackexchange.com/questions/137864/… – kamfulebu Mar 7 '15 at 0:11
• Are you trying to consistently multiply the values of one row in a column with the values of the same row in another column? – BritishSteel Mar 7 '15 at 0:14
• I'm still not sure what you want. Do you want to update the columns by col_def, and then populate another column by their sum, or just calculate the sum of other columns and then update a sum column? – recurvata Mar 7 '15 at 0:19
• that, to try, multiply the values of one row and a column with the values of the same row and another column ... but I'm an absolute beginner so I do not go :) – kamfulebu Mar 7 '15 at 0:21

## 1 Answer

I think I understand what you're trying to do. You are close, but can consolidate this with list comprehension:

``````with arcpy.da.UpdateCursor(fc, (fieldNameList)) as cursor:

for row in cursor:

hi = sum([row[i]*row for i in range(len(fieldNameList))])
row = hi
cursor.updateRow(row)
``````

However, you need to realize that at this point the value of row is also being multiplied by itself and added to the sum, because you iteration is running for the length of the entire row. Also, if there's a value at row it will also be multiplied and reflected in the sum. Because you have hard-coded the index numbers 14 and 15, it seems like you know the length of the list of field names, so you can hard code the range in the list comprehension like so:

``````        hi = sum([row[i]*row for i in range(14)])
``````

Now only the field values UP TO the field at index position 14 will be multiplied and summed, and 14 and 15 will be left out.

List comprehension is super great.

• Very nice code and understanding the OP question. I wasn't sure of it myself. – recurvata Mar 7 '15 at 0:21
• Yeah, the for loop gave me a good clue at what was intended, but it is a tricky operation to describe anyway. – mr.adam Mar 7 '15 at 0:25
• @kamfulebu, it would be nice if you marked mr.adam answer as correct, both for his benefit in reputation and more importantly, to let others with similar questions find answers. – recurvata Mar 7 '15 at 0:31
• yes, thanks @recurvata. Also, I was just assuming that row was the last item in the row, i.e. the final field listed in the fieldNameList, and that the multiplier, row is in the second-to-last position. If that isn't so, I would recommend modifying the list because it'll make things easier. – mr.adam Mar 7 '15 at 0:35
• Ok, I solved the problem using numpy: hi = sum(np.array([row[i]*row for i in range(14)])**2) – kamfulebu Mar 7 '15 at 1:46