title: Efficient way to find the min and max value of a changing set in python
body: <p>I need to find the min/max value in a changing large set, in C++, it could be</p>
  
  <pre><code>#include&lt;set&gt;
  using namespace std;
  int minVal(set&lt;int&gt; &amp; mySet){
      return *mySet.begin();
  }
  int maxVal(set&lt;int&gt; &amp; mySet){
      return *mySet.rbegin();
  }
  int main(){
      set &lt;int&gt; mySet;
      for(..;..;..){
         // add or delete element in mySet
         ...
         // print the min and max value in the set
         printf("%d %d\n", minVal(mySet), maxVal(mySet)); 
      }
  }
  </code></pre>
  
  <p>In C++, each query operation is O(1), but in python, I tried to use the build-in method min and max but it's too slow. Each min/max operation takes O(n) time (n is the length of my Set). Are there any elegant and efficient way to do this? Or any datatype support these operation?</p>
  
  <pre><code>mySet=set()
  for i in range(..):
    # add or delete element in mySet
    ...
    # print the min and max value in the set
    print(min(mySet),max(mySet))
  </code></pre>