title: How to find "nearest neighbors" in a list in Python?
body: <p>My objective is the following: given a list of numbers (int. or float) (1D), I need to find <em>N</em> amount of numbers that are closest to<br />
  each other in this list. For example:</p>
  <p>Given <code>sample_list = [1, 8, 14, 15, 17, 100, 35]</code> and <code>N = 3</code>, the desired function should return a list of <code>[14, 15, 17]</code>, because the 3 &quot;nearest neighbors&quot; in this list are 14, 15, and 17.</p>
  <p>So far, I have tackled the problem the following way, being forced to make <strong>several compromises</strong> in my approach (after unsuccessfully searching for a solution online):</p>
  <p>I decided to iterate over each number in the list and find its <em>N</em> amount of &quot;nearest neighbors&quot;. These &quot;nearest neighbors&quot; are stored in a new list and then compared with other such lists using <a href="https://numpy.org/doc/stable/reference/generated/numpy.diff.html" rel="nofollow noreferrer">numpy.diff</a> function. The list with the smallest amount resulted from <code>numpy.diff</code> is the winner.</p>
  <p>At the base of my solution is the code provided by <a href="https://www.geeksforgeeks.org/python-find-closest-number-to-k-in-given-list/" rel="nofollow noreferrer">this article</a> on geeksforgeeks.org. The idea to use <code>numpy.diff</code> function came from <a href="https://stackoverflow.com/questions/7172933/calculate-difference-between-adjacent-items-in-a-python-list">here</a> on stackoverflow.com</p>
  <p>My (working) monstrosity can be found below:</p>
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  def find_nn_in_list_1D(list_for_search, num_neighbors_desired):
  
          list_iterator = list_for_search.copy()
  
          list_mod = list_for_search.copy()
  
          neighbor_storage_list = []
  
          neighbor_eval_score = 999_999_999
                  
          for number in list_iterator:
                  
                  neighbor_nil = list_mod[min(range(len(list_mod)), key = lambda i: abs(list_mod[i] - number))]
                  
                  neighbor_storage_list.append(neighbor_nil)
  
                  list_mod.remove(neighbor_nil)
          
                  for num_neighbor in range(num_neighbors_desired):
                  
                          neighbor = list_mod[min(range(len(list_mod)), key = lambda i: abs(list_mod[i] - number))]
                                  
                          neighbor_storage_list.append(neighbor)
                                  
                          list_mod.remove(neighbor)
                  
                  neighbor_storage_list.sort()
  
                  if neighbor_eval_score &gt; abs(sum(np.diff(neighbor_storage_list))):
                                  
                          neighbor_eval_score = abs(sum(np.diff(neighbor_storage_list)))
                          
                          nearest_neighbors_list = neighbor_storage_list.copy()
                                                          
                  list_mod = list_for_search.copy()
                          
                  neighbor_storage_list = []
  
          return nearest_neighbors_list
  </code></pre>
  <p>I am very certain that there is <strong>a much better way</strong> of solving this issue.</p>
  <p>Even though it works, I would like to write better, cleaner code.</p>
  <p>If you have a better solution than mine please share below. Thank you for your time!</p>