How to remove outer list?
<p>I have a simple example,
Create a program that asks the user for a number and then prints out a list of all the divisors of that number.</p>

<p>And i am solving it like this:</p>

<pre><code>n = 4
list_range = list(range(1,n+1))
divisor_list = []
divisor_list.append([i for i in list_range if n%i==0])
print divisor_list
#output:
#[[1, 2, 4]]
</code></pre>

<p>I want the output to be <code>[1, 2, 4]</code></p>

<p>I can achieve this by:</p>

<pre><code>n = 4
list_range = list(range(1,n+1))
divisor_list = []
for i in list_range:
    if n % i == 0:
        divisor_list.append(i)
print divisor_list
#output:
#[1, 2, 4]
</code></pre>

<p>But is there a better way of achieving this ?</p>

<p>You don't need to initialize <code>divisor_list</code> as an empty sequence at all. The comprehension you're appending is the actual answer you're looking for.</p>

<pre><code>divisor_list = [i for i in list_range if n%i==0]
</code></pre>

python list python-2.7