list comprehension in python with if and else
<p>after i test,</p>

<pre><code>len_stat = [len(x) if len(x) &gt; len_stat[i] else len_stat[i] for i, x in enumerate(a_list)]
</code></pre>

<p>is same with </p>

<pre><code>for i, x in enumerate(a_list):
    if len(x) &gt; len_stat[i]:
        len_stat[i] = len(x)
</code></pre>

<p>of course, but,</p>

<pre><code>len_stat = [len(x) if len(x) &gt; len_stat[a_list.index(x)] else len_stat[a_list.index(x)] for x in a_list]
</code></pre>

<p>is different with</p>

<pre><code>for x in a_list:
    if len(x) &gt; len_stat[a_list.index(x)]:
        len_stat[a_list.index(x)] = len(x)
</code></pre>

<p>later, i know <code>&lt;list&gt;.index</code> is a bad method used here.</p>

<p>but why they are different in last two examples?</p>

<hr>

<p>my fault! here is my test code,</p>

<pre><code>a_list = ['Bacteroides fragilis YCH46', 'YCH46', '20571', '-', 'PRJNA13067', 'FCB group', 'Bacteroidetes/Chlorobi group', 'GCA_000009925.1 ', '5.31099', '43.2378', 'chromosome:NC_006347.1/AP006841.1; plasmid pBFY46:NC_006297.1/AP006842.1', '-', '2', '4717', '4625', '2004/09/17', '2016/08/03', 'Complete Genome', 'ftp://ftp.ncbi.nlm.nih.gov/genomes/all/GCF_000009925.1_ASM992v1', 'ftp://ftp.ncbi.nlm.nih.gov/genomes/all/GCA_000009925.1_ASM992v1']

len_stat_1 = [0 for x in a_list]
len_stat_2 = [0 for x in a_list]
len_stat_3 = [0 for x in a_list]
len_stat_4 = [0 for x in a_list]

len_stat_1 = [len(x) if len(x) &gt; len_stat_1[i] else len_stat_1[i] for i, x in enumerate(a_list)]

for i, x in enumerate(a_list):
    if len(x) &gt; len_stat_2[i]:
        len_stat_2[i] = len(x)

len_stat_3 = [len(x) if len(x) &gt; len_stat_3[a_list.index(x)] else len_stat_3[a_list.index(x)] for x in a_list]

for x in a_list:
    if len(x) &gt; len_stat_4[a_list.index(x)]:
        len_stat_4[a_list.index(x)] = len(x)

print len_stat_1
print len_stat_2
print len_stat_3
print len_stat_4
</code></pre>

<p>output:</p>

<pre><code>[26, 5, 5, 1, 10, 9, 28, 16, 7, 7, 72, 1, 1, 4, 4, 10, 10, 15, 63, 63]
[26, 5, 5, 1, 10, 9, 28, 16, 7, 7, 72, 1, 1, 4, 4, 10, 10, 15, 63, 63]
[26, 5, 5, 1, 10, 9, 28, 16, 7, 7, 72, 1, 1, 4, 4, 10, 10, 15, 63, 63]
[26, 5, 5, 1, 10, 9, 28, 16, 7, 7, 72, 0, 1, 4, 4, 10, 10, 15, 63, 63]
</code></pre>

<p>as you can see, the last two is different!</p>

<p>it really confused me.</p>

<p>This list comprehension:</p>

<pre><code>len_stat = [len(x) if len(x) &gt; len_stat[a_list.index(x)] else len_stat[a_list.index(x)] for x in a_list]
</code></pre>

<p>Is equivalent to:</p>

<pre><code>temp = []
for x in a_list:
    temp.append(len(x) if len(x) &gt; len_stat[a_list.index(x)] else len_stat[a_list.index(x)])
len_stat = temp
</code></pre>

<p>Which is equivalent to:</p>

<pre><code>temp = []
for x in a_list:
    if len(x) &gt; len_stat[a_list.index(x)]:
        val = len(x)
    else:
        val = len_stat[a_list.index(x)]
    temp.append(val)
len_stat = temp
</code></pre>

<p>Equivalent, of course, except for the <code>temp</code> list. The comprehension approach will replace <code>len_stat</code> with a new list, that will always be the same length as <code>len_stat</code> that will have either <code>len(x)</code> for every x in <code>len_stat</code> or will have <code>len_stat[a_list.index(x)]</code>. The for-loop you wrote will mutate <code>len_stat</code> based on the conditional, and it's hard to say exactly what will happen without knowing the content of your lists, but it could potentially change values of <code>len_stat</code> all over the place.</p>

<p><h2> To be clear</h2> I do <strong>not</strong> recommend this approach, but I am trying to illustrate how the comprehension is different than the for-loop.</p>

python