min () expected sequence error only happens for newly created objects in edit mode I wrote a script that selects the selected object's vertices with the lower Z value, and set's the origin of the object to the median point.
Everything worked perfectly until I tried to make things a bit better, by returning the 3D view mode to the mode that was used before running the script as in Edit Mode or Object Mode... etc.

The error I'm getting happens in a very specific case, and I couldn't find no logical explanation to it. I have "New Object in Edit Mode" enabled in my user preferences, so when I create a new object and run this scrip immediately, I'm getting an error:

 min() arg is an empty sequence


This error only occurs when an new object is created in Edit Mode, then I run the script immediately. 
I tried disabling creating new object in Edit Mode, the error disappeared! 
Even if create new object in Edit Mode is enabled, the error wont occur if I press tab to go to Object Mode first, then tab again to go back to Edit Mode, then run the script.
This error will not occur in any other cases whether run in Object Mode or Edit Mode.
I tried saving right after creating a new object in Edit Mode, then run the script, no error!
This is a very weird situation to the programming novice me, so what am I doing wrong here? help appreciated in advance, blend file attached.
Edit: I found a solution by unchecking "create in edit mode" off in user preferences, then checking it back after applying the rest of the code in the example, but I want to understand why is this error happening.
You need to update your edits if you are not using bmesh. Add these lines first to your def:

if bpy.context.object.mode == "EDIT":
    bpy.context.object.update_from_editmode()