You could use the built-in min
function with an anonymous function to access value parameter:
min(listNodes, key=lambda x: x.value)
Or you can define the rich comparison methods __lt__
and __eq__
in Node
(you can define additional rich comparison methods, but these two are sufficient for ordering like min
):
class Node:def __init__(self, node, value, left=None, right=None):
self.node = node
self.value = value
self.left = left
self.right = right
self.code = ''
def __iter__(self):
return self
def __lt__(self, other):
return self.value < other.value
def __eq__(self, other):
return self.value == other.value
which will allow min
to directly compare the nodes finding the minimum!
min(listNodes)
Before Python3, it was possible to use the now-deprecated __cmp__
method to create all the rich comparison methods at once; see Question About This and Linked Official Notes