title: Python nested loop and update set
body: <p>I am a new student and now I am learning diversity in a set. I want to implement the greedy heuristic algorithm, this algorithm is quite easy, but I am stuck in the code. </p>
  
  <p>Greedy heuristics generally make use of two sets: the set X of available items and the set S which contains the selected ones. Items are iteratively moved from X to S and vice versa until |S| = k. Items are moved one-by-one from X to S until k of them have been selected. The item that is moved each time is the one that has the maximum item-set distance <code>setdist</code> from S. The item-set distance/<code>setdist</code>, denoted as following: </p>
  
  <p><a href="https://i.stack.imgur.com/xg27V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xg27V.png" alt="enter image description here"></a></p>
  
  <p>The algorithm looks like this:
   <a href="https://i.stack.imgur.com/wHWxf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wHWxf.png" alt="enter image description here"></a></p>
  
  <p>This is my current code: </p>
  
  <pre><code>from scipy.spatial import distance
  
  k = 5
  
  # this is just sample set, the real set is complex. 
  S = [1,9]
  X = [3,4,5,6,7,8,103,1035,2009]
  
  i = 1
  while i &lt; k:
      mylist =[]
      for a in range(0, len(X)):
          d = 0
          for b in range(0, len(S)):
              ed = distance.euclidean(X[a],S[b])
              if ed &gt; d:
                  d = ed
                  listku = [(X[a],S[b]), d]
                  #print(listku)
                  mylist.append(listku)
                  max_dis = max(mylist,key=lambda item:item[1])
      i = i + 1
  </code></pre>
  
  <p>From that code, I can get the max_dis, which is <code>[(2009, 1), 2008.0]</code> and I am stuck here. </p>
  
  <p>The next step is, I want to take <code>2009</code> from <code>max_dis [(2009, 1), 2008.0]</code> then append to <code>S</code> and looping iteratively. </p>
  
  <p>The expected output maybe: <code>[1,9,103,1035,2009]</code> this set is the optimum one/has max diverse.</p>