Python: get list with nearest dates and same size for list with dates and values
<p>I am trying to get list of nearest dates for current list of lists (dates and value) </p>

<p>Have two lists:</p>

<pre><code>[["20160901", 0.244], ["20160902", 0.232], ["20160906", 0.214],
["20160909", 0.235], ["20160910", 0.244], ["20160911", 0.271],     
["20160912", 0.239], ["20160914", 0.25], ...]
</code></pre>

<p>length is X</p>

<p>and:</p>

<pre><code>[["20160907", -9.39979076385498, -6.318868160247803], 
["20160913", -10.568793296813965, -6.815752029418945], ... ]
</code></pre>

<p>length is Y</p>

<p>Need to get first list with length is Y and nearest dates for second list.</p>

<p>e.g. </p>

<pre><code>[["20160906", 0.214], ["20160912", 0.239], ...]
</code></pre>

<p>Here is my code:</p>

<pre><code>def nearest_date(dates, pivot):
    return min(dates, key=lambda x: abs(x - pivot))
</code></pre>

<p>But I get lists different sizes.</p>

<pre><code>list_dates = [["20160901", 0.244], ["20160902", 0.232], ["20160906", 0.214],
["20160909", 0.235], ["20160910", 0.244], ["20160911", 0.271],     
["20160912", 0.239], ["20160914", 0.25]]

list_comp = [["20160907", -9.39979076385498, -6.318868160247803], 
["20160913", -10.568793296813965, -6.815752029418945]]

[ min(list_dates, key = lambda x: abs(int(x[0])-int(p[0]))) for p in list_comp ]
</code></pre>

<p>result :</p>

<pre><code>[['20160906', 0.214], ['20160912', 0.239]]
</code></pre>

python