Replace Values in Dictionary
<p>I am trying to replace the <code>values</code> in the <code>lines dictionary</code> by the <code>values of the corresponding key</code> in the <code>points dictionary</code></p>

<pre><code>lines = {'24': ('2', '10'), '25': ('17', '18')} #k,v =&gt; Line ID(Points) 
points = {'10': ('2.416067758476', '2.075872272548'), '17': ('2.454725131264', '5.000000000000'), '18': ('1.828299357105', '5.000000000000'), '2': ('3.541310767185', '2.774647545044')} #Point ID =&gt; (X,Y)

i = 1
while i in range(len(lines)):
    for k, v in lines.iteritems():
        for k1, v1 in points.iteritems():
            for n, new in enumerate(v):
                if k1 == new:
                    lines[k] = v1
i += 1
</code></pre>

<p>The expected output is:</p>

<pre><code>lines = {'24': ('2.416067758476', '2.075872272548', '3.541310767185', '2.774647545044'), '25': ('2.454725131264', '5.000000000000', '1.828299357105', '5.000000000000')}
</code></pre>

<p>The output from the above code</p>

<pre><code>lines = {'24': ('3.541310767185', '2.774647545044'), '25': ('1.828299357105', '5.000000000000')}
</code></pre>

<p>If i create a list, then append that into the <code>lines[k]</code> i end up getting the all the values in the points list as that of each key element of lines. I think it is not looping out of the for loop properly. </p>

<p>Just use a dictionary comprehension:</p>

<pre><code>&gt;&gt;&gt; {k:points[v[0]]+points[v[1]] for k,v in lines.items()}
{'24': ('3.541310767185', '2.774647545044', '2.416067758476', '2.075872272548'), '25': ('2.454725131264', '5.000000000000', '1.828299357105', '5.000000000000')}
</code></pre>

python python-2.7 dictionary