Manipulating list of lists and dictionary
<p>I have a list of lists representing the keys in a dictionary. I wish to pickup the smaller key for each list in lists. For instance,</p>

<pre><code>L1 = [['1_A','2_A'],['1_B','2_B']]
D1 = {'1_A': 0.22876, '2_A': 0.22382, '1_B': 0.2584, '2_B': 0.25373}

for li in L1:
    for ll in li:
        if ll in D1.keys():
            print "Value for %s is %s" %(ll,D1[ll])
        else:
            print "Values not found"
</code></pre>

<p>When I print it, I get:</p>

<pre><code>Value for 1_A is 0.22876
Value for 2_A is 0.22382
Value for 1_B is 0.2584
Value for 2_B is 0.25373
</code></pre>

<p>The output I expect is <code>2_A</code>, <code>2_B</code> since both of them have smaller values compared to <code>1_A</code> and <code>1_B</code> respectively. Can anyone suggest how to do this?</p>

<p>I found another simple answer !</p>

<pre><code>L1 = [['1_A','2_A'],['1_B','2_B']]
D1 = {'1_A': 0.22876, '2_A': 0.22382, '1_B': 0.2584, '2_B': 0.25373}

for i in L1:
    val = (sorted(i, key=D1.get))[0]
    newlist.append(val)

"newlist = ['2_A', '2_B']"
</code></pre>

<p>A list comprehension version:</p>

<pre><code>newlist = [sorted(i,key=D1.get)[0] for i in L1]
</code></pre>

<p>Thanks !</p>

python python-2.7 dictionary