title: Applying a method with no return value to each element of a list
body: <p>Is there a way to use methods with no return value such as random.shuffle in a list comprehension?</p>
  
  <pre><code>&gt;&gt;&gt; import pprint
  &gt;&gt;&gt; import random
  &gt;&gt;&gt; 
  &gt;&gt;&gt; L = [ random.shuffle(range(5)) for x in range(5)]
  &gt;&gt;&gt; 
  &gt;&gt;&gt; print L
  [None, None, None, None, None]
  </code></pre>
  
  <p>This is the for loop that applies the random.shuffle method to each item of my list:</p>
  
  <pre><code>&gt;&gt;&gt; L = [ range(5) for x in range(5) ]
  &gt;&gt;&gt; pprint.pprint(L)
  [[0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4]]
  &gt;&gt;&gt; for element in L:
  ...     random.shuffle(element)
  ... 
  &gt;&gt;&gt; pprint.pprint(L)
  [[2, 0, 3, 1, 4],
   [2, 0, 1, 4, 3],
   [4, 1, 3, 0, 2],
   [1, 2, 4, 3, 0],
   [1, 3, 0, 2, 4]]
  </code></pre>
  
  <p>I can use map, which as a side effect shuffles the original list but returns a list of <code>None</code></p>
  
  <pre><code>&gt;&gt;&gt; L = [ range(5) for x in range(5) ]
  &gt;&gt;&gt; pprint.pprint(L)
  [[0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4]]
  &gt;&gt;&gt; map(random.shuffle, L)
  [None, None, None, None, None]
  &gt;&gt;&gt; pprint.pprint(L)
  [[3, 0, 4, 1, 2],
   [2, 3, 0, 1, 4],
   [2, 3, 1, 4, 0],
   [4, 2, 0, 3, 1],
   [1, 3, 0, 2, 4]]
  </code></pre>
  
  <p>as does using the list comprehension with shuffle:</p>
  
  <pre><code>&gt;&gt;&gt; L = [ range(5) for x in range(5) ]
  &gt;&gt;&gt; pprint.pprint(L)
  [[0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4]]
  &gt;&gt;&gt; L1 = [ random.shuffle(x) for x in L ]
  &gt;&gt;&gt; pprint.pprint(L1)
  [None, None, None, None, None]
  &gt;&gt;&gt; pprint.pprint(L)
  [[1, 4, 0, 2, 3],
   [0, 4, 1, 3, 2],
   [2, 3, 4, 0, 1],
   [4, 1, 0, 2, 3],
   [2, 0, 4, 3, 1]]
  </code></pre>
  
  <p>Many questions and answers on stack overflow already point out that using map or a lc for the side effect is bad practice. I was wondering if there's any correct way to use a method with no return value in a list comprehension.</p>
  
  <p>Is writing a method to wrap the non-returning method the only way:</p>
  
  <pre><code>&gt;&gt;&gt; def shuffled(L):
  ...     ret_val = L[:]
  ...     random.shuffle(ret_val)
  ...     return ret_val
  ... 
  &gt;&gt;&gt; L = [ shuffled(range(5)) for x in range(5)]
  &gt;&gt;&gt; pprint.pprint(L)
  [[2, 1, 0, 4, 3],
   [4, 0, 3, 1, 2],
   [4, 2, 3, 0, 1],
   [1, 0, 4, 2, 3],
   [2, 4, 3, 0, 1]]
  &gt;&gt;&gt; 
  </code></pre>