-2

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)

def remove(list1,num)

    list1=list1
    num = num

This is what is given. So far, I have this:

def remove(list1,num)

list1=list1

num=num

if num in list1:

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.

Would appreciate the help.

chepner
  • 497,756
  • 71
  • 530
  • 681
Jessica
  • 139
  • 2
  • 9
  • 1
    can you use list comprehension for this? `new_list = [i for i in list1 if i != num]` – David Zemens Oct 18 '16 at 15:53
  • @DavidZemens I was typing this solution but you came up first so go ahead. – MooingRawr Oct 18 '16 at 15:54
  • Since it's a looping question, I imagine you're supposed to do something where you make a new list, and then loop over the original adding each element to the new list as long as it is not equal to `num` – Travis Griggs Oct 18 '16 at 15:54
  • You need to fix your indentation (and missing `:`) in your sample code – Wayne Werner Oct 18 '16 at 15:55
  • Possible duplicate of [Remove items from a list while iterating in Python](http://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating-in-python) – David Zemens Oct 18 '16 at 15:57

5 Answers5

2

It sounds like this is a homework problem, especially since you can't use .remove.

Given that, your teacher probably wants you to take a manual approach that looks something like this:

  1. Create a new list
  2. For each item in the previous list...
    1. If it's not the value you want to filter out, .append it to your new list
  3. Return your new list

(mouseover if you don't want to write the code yourself)

def remove(list1, num):
    new_list = []
    for item in list1:
        if item != num:
            new_list.append(item)
    return new_list
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
1

Use list comprehension:

list1 = [1,2,3]
num = 2
new_list = [i for i in list1 if i != num]
print(new_list)
>> [1,3]
David Zemens
  • 53,033
  • 11
  • 81
  • 130
0
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]

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.

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

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 example_list=[1,2,3] and we are on the second iteration of the for loop and we remove 2 in place, it will try to go somewhere we don't want it to go.

Graham
  • 7,431
  • 18
  • 59
  • 84
MooingRawr
  • 4,901
  • 3
  • 24
  • 31
0

Taking into account:

list=[1,2,3,2]

You can check if the element is in the list with:

if num in list

And then remove with:

list.remove(num)

And iterate through it

Example:

>>> list=[1,2,3]
>>> list.remove(2)
>>> print list
[1, 3]
ppanero
  • 327
  • 5
  • 17
0

To go with pure loops and delete with list index:

#!/usr/bin/env python
from __future__ import print_function

def remove(item, old_list):
    while True:
        try:
            # find the index of the item
            index = old_list.index(item)
            # remove the item found
            del old_list[index]
        except ValueError as e:
            # no more items, return
            return old_list

a_list = [1, 2, 3, 2, 1, 3, 2, 4, 2]
print(remove(2, a_list))

If possible, of course, you should use list comprehension, which is pythonic and much easier!

Minghao Ni
  • 185
  • 1
  • 9