Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen to drop list Comprehension and the Pythonic way?
    primarykey
    data
    text
    <p>I created a line that appends an object to a list in the following manner</p> <pre><code>&gt;&gt;&gt; foo = list() &gt;&gt;&gt; def sum(a, b): ... c = a+b; return c ... &gt;&gt;&gt; bar_list = [9,8,7,6,5,4,3,2,1,0] &gt;&gt;&gt; [foo.append(sum(i,x)) for i, x in enumerate(bar_list)] [None, None, None, None, None, None, None, None, None, None] &gt;&gt;&gt; foo [9, 9, 9, 9, 9, 9, 9, 9, 9, 9] &gt;&gt;&gt; </code></pre> <p>The line </p> <pre><code>[foo.append(sum(i,x)) for i, x in enumerate(bar_list)] </code></pre> <p>would give a pylint W1060 Expression is assigned to nothing, but since I am already using the foo list to append the values I don't need to assing the List Comprehension line to something.</p> <p><strong>My questions is more of a matter of programming correctness</strong></p> <p>Should I drop list comprehension and just use a simple for expression?</p> <pre><code>&gt;&gt;&gt; for i, x in enumerate(bar_list): ... foo.append(sum(i,x)) </code></pre> <p>or is there a correct way to use both list comprehension an assign to nothing?</p> <p><strong>Answer</strong></p> <p>Thank you @user2387370, @kindall and @Martijn Pieters. For the rest of the comments I use append because I'm not using a list(), I'm not using i+x because this is just a simplified example. </p> <p>I left it as the following:</p> <pre><code>histogramsCtr = hist_impl.HistogramsContainer() for index, tupl in enumerate(local_ranges_per_histogram_list): histogramsCtr.append(doSubHistogramData(index, tupl)) return histogramsCtr </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload