"None" string appear after print a line in python
<p>I a new in python programming. Today I practice with sorting a list. 
I've got a strange output with this code:</p>

<pre><code># problem 13
def lensort(list):
   list.sort(key=lambda x: len(x))
   print list 

print "Sort by length: ", lensort(['python', 'perl', 'java', 'c', 'haskell','ruby'])
</code></pre>

<p>The output is: </p>

<pre><code>Sort by length:  ['c', 'perl', 'java', 'ruby', 'python', 'haskell']
None
</code></pre>

<p>So, I have no idea the reason why "None" is shown in 2nd line.
Could anyone help to about this problem?</p>

<p>Thank you very much!</p>

<p>Because you are printing the list at the end of <code>lensort</code> function instead of returning it. A function in Python implicitly returns <code>None</code> if it doesn't <code>return</code> anything explicitly.</p>

<pre><code>def lensort(list):
    list.sort(key=lambda x: len(x))
    return list 
</code></pre>

<p>By the way, you can simplify the key function passed as follows: <code>list.sort(key=len)</code></p>

<p>Also, avoid using class names as a variable name such as <code>list</code>, <code>set</code>, <code>dict</code> or <code>object</code>:</p>

<pre><code>def lensort(lst):
    ...
</code></pre>

python sorting