How to change map into a loop?
<p>this question is probably really simple but it will help me to understand the difference between loop and map.
In the first example I managed to change it from:</p>

<pre><code>    nrs = list(map(lambda x: int( open("img_" + str(x) + ".csv").readline().split(",")[1] ) , range(82))
</code></pre>

<p>to:</p>

<pre><code>   nrs = [] 
   for x in range(82):
       nrs.append(int( open("img_" + str(x) + ".csv").readline().split(",")[1] ))
</code></pre>

<p>but how can I change:</p>

<pre><code>    plt.plot(list(map(lambda x: ages[x], list(avg_si.keys() ))), list(avg_si.values()), 'ro', label='size')
</code></pre>

<p>into a loop?
I tried with:</p>

<pre><code>srednia = []
for x in list(avg_si.values()):
     srednia.append(ages[x], list(avg_si.keys()))
plt.plot(srednia, "ro", label = "size")
</code></pre>

<p>But I get a KeyError </p>

<p>The expression</p>

<pre><code>plt.plot(list(map(lambda x: ages[x], list(avg_si.keys() ))), list(avg_si.values()), 'ro', label='size')
</code></pre>

<p>is somewhat hard to parse. In addition to the last two string arguments you are passing it two lists:</p>

<pre><code>1) list(map(lambda x: ages[x], list(avg_si.keys() )))
2) list(avg_si.values())
</code></pre>

<p>The second list requires no comment. The first one is the <code>map</code> expression. It is equivalent to the following list comprehension:</p>

<pre><code>[ages[x] for x in avg_si.keys()]
</code></pre>

<p>Which would make the entire line equivalent to</p>

<pre><code>plt.plot([ages[x] for x in avg_si.keys()], list(avg_si.values()), 'ro', label='size')
</code></pre>

<p>If you wanted to replace the comprehension by a loop you could do:</p>

<pre><code>ageList = []
for x in avg_si.keys():
    ageList.append(ages[x])
</code></pre>

<p>and then:</p>

<pre><code>plt.plot(ageList, list(avg_si.values()), 'ro', label='size')
</code></pre>

python