Select to view content in your preferred language

# Find max value in a field.

11565
8
10-13-2017 01:42 PM
Regular Contributor

Why do I get a print out of all the records values as opposed to just the highest which should be 8182 ?

``````with arcpy.da.SearchCursor("escroads",'RecID') as cursor:
for row in cursor:
print str(max(row))‍‍‍``````
1 Solution

Accepted Solutions
MVP Emeritus
``````field_vals = [1, 1000, 10, 100, 100]

search_cursor = [row_ for row_ in field_vals]

max(search_cursor)
Out[4]: 1000‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````

you have to start thinking of you search cursor in this way.

or this way.

``````big_num = -1e200

for row_ in field_vals:
print("{} bigger than {}?".format(row_, big_num))

1 bigger than -1e+200?
1000 bigger than -1e+200?
10 bigger than -1e+200?
100 bigger than -1e+200?
100 bigger than -1e+200?
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````

or do something as you go along

``````for row_ in field_vals:
print("{} bigger than {}?".format(row_, row_*20))

1 bigger than 20?
1000 bigger than 20000?
10 bigger than 200?
100 bigger than 2000?
100 bigger than 2000?‍‍‍‍‍‍‍‍``````
8 Replies
Frequent Contributor

Calling max() on a row will return the maximum value within the fields in that row, not the maximum across multiple rows.

MVP Emeritus
``````field_vals = [1, 1000, 10, 100, 100]

search_cursor = [row_ for row_ in field_vals]

max(search_cursor)
Out[4]: 1000‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````

you have to start thinking of you search cursor in this way.

or this way.

``````big_num = -1e200

for row_ in field_vals:
print("{} bigger than {}?".format(row_, big_num))

1 bigger than -1e+200?
1000 bigger than -1e+200?
10 bigger than -1e+200?
100 bigger than -1e+200?
100 bigger than -1e+200?
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````

or do something as you go along

``````for row_ in field_vals:
print("{} bigger than {}?".format(row_, row_*20))

1 bigger than 20?
1000 bigger than 20000?
10 bigger than 200?
100 bigger than 2000?
100 bigger than 2000?‍‍‍‍‍‍‍‍``````
Regular Contributor

Thanks

Interesting, this is the most I have used cursors.

I get the desired result now, including the comma for some reason

for row in cursor:
print max(cursor)

(8182,)

MVP Emeritus

max of a tuple I suspect

max((8131,))
Out[16]: 8131

check whether it works on the RecID might not be good if they are sorted...

MVP Esteemed Contributor

``````with arcpy.da.SearchCursor("escroads",'RecID') as cursor:
print max(cursor)‍‍``````

The Python max function is designed to work on iterables, which a Python cursor is a form of an iterable.  Your original code sets up the cursor/iterable, but the for loop moves the iterator forward one position, and then max is called against the remainder of the iterable, exhausting the cursor.  If the maximum value just happens to be the first record, it will not be returned as the maximum value because max never knew the value existed.

Cursors are extremely powerful, I encourage you to learn more about them and also Python iterables.  I started (but haven't finished yet) an "Iterable Cursor" blog series that might help someone new to ArcPy cursors understand how to think of them as Python iterables: /blogs/tilting/2015/11/24/the-iterable-cursor-iteration-basics.

Regular Contributor

Invaluable advice I will look into this more.

Thanks.

by Anonymous User
Not applicable

I took this concept a step or two further and developed this little one-line gem which uses list comprehension and treats the cursor like a tuple:

`max([cur[0] for cur in arcpy.da.SearchCursor("Asset Layer", "ASSETID")])`

Important notes: This bit of arcpy code works in ArcMap, but it fails on an empty layer because the list is empty.  Also, since it's in ArcMap, the code above honors the user's selected rows or features and therefore returns the maximum number of the field within the selection.

Regular Contributor

@Anonymous User This worked perfectly, thanks!