removing a number from a list
<p>I'm stuck on a part of a looping question. I have to remove from list1 all instances of "number". So lets say list1 is (1,2,3) and num is 2. The list I would have to return would be (1,3)</p>

<pre><code>def remove(list1,num)

    list1=list1
    num = num
</code></pre>

<p>This is what is given. 
So far, I have this:</p>

<pre><code>def remove(list1,num)

list1=list1

num=num

if num in list1:
</code></pre>

<p>This is where I'm stuck because I don't know how to say in coding "remove num from list" 
I am not allowed to use .remove either. </p>

<p>Would appreciate the help.</p>

<pre><code>def remove(item,given_list):
    new_list = []
    for each in given_list:
        if item != each:
            new_list.append(each)
    return new_list

print(remove(2,[1,2,3,1,2,1,2,2,4]))
#outputs [1, 3, 1, 1, 4]
</code></pre>

<p>While my answer as strong as others, I feel this is the fundamental way of thinking on how to remove an item from a list. It allows people to understand at a basic level of what's happening. </p>

<p>Basically we are taking 2 inputs, the item to remove and the list to remove it from. It loops through the <code>input_list</code> and checks if the <code>item</code> is equal to the <code>item</code> we want to remove, if it's not the same we <code>append</code> it to the new list, and return the new list. </p>

<p>We don't want to remove items in place in the list while looping because it could cause undesirable looping. For example if we have the <code>example_list=[1,2,3]</code> and we are on the second iteration of the <code>for loop</code> and we remove 2 in place, it will try to go somewhere we don't want it to go. <a class='doc-link' href="http://stackoverflow.com/documentation/python/3553/common-pitfalls/12259/list-multiplication-and-common-references#t=201610181559287156669">See this for more detailed example</a></p>

python loops for-loop