To remove items from a list? | Newbie | | Join Date: Mar 2009
Posts: 4
| |
i am trying to remove items in a list if the items do not exist in another list but my program does not work the way i have expected
for example: -
l = ['a','b','c','d','e','f','g','h', 'i', 'j','k']
-
l2 = ['d', 'e', 'i'] # l2 could be a list of raw inputs
-
-
for x in l:
-
if x not in r:
-
l.remove(x)
-
print l
-
logically speaking, all the letters other than 'd', 'e', and 'i' shoulde be removed, but the program returns ['b', 'd', 'e', 'g', 'i', 'k']. it seems the program deletes the first item in the list, skip the next item, then repeat.
what went wrong here? is there another way to do it?
|  | Familiar Sight | | Join Date: Mar 2008 Location: Belgium
Posts: 137
| | | re: To remove items from a list?
Tricky problem :)
After trying a while, before giving up I though about using 2 identical lists. -
l = ['a','b','c','d','e','f','g']
-
l2 = ['a','b']
-
l3 = ['a','b','c','d','e','f','g']
-
for x in l:
-
if x not in l2:
-
l3.remove(x)
-
I know this isn't ideal for a lot of reasons, but that is the best I could come up with
| | Newbie | | Join Date: Apr 2009
Posts: 2
| | | re: To remove items from a list?
no love for filter? you are filtering after all ;)
two possibilities:
x =[list to be filtered]
y =[list of terms to be filtered out]
filter(lambda x: x not in y,x)
or
[p for p in x if p not in y]
et voila.
list comp is probably faster (i haven't tested)
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,565
| | | re: To remove items from a list?
Very good benjumanji. The filter() solution is fine, but I prefer the list comprehension due to its speed and readability. The list comprehension was twice as fast when tested with timeit().
|  | Familiar Sight | | Join Date: Mar 2008 Location: Belgium
Posts: 137
| | | re: To remove items from a list?
in all honesty, that is the trouble I have with python, you have a few millions way to do the same thing :)
anyway, good to learn another new trick :)
| | Newbie | | Join Date: Mar 2009
Posts: 4
| | | re: To remove items from a list? Quote:
Originally Posted by benjumanji no love for filter? you are filtering after all ;)
two possibilities:
x =[list to be filtered]
y =[list of terms to be filtered out]
filter(lambda x: x not in y,x)
or
[p for p in x if p not in y]
et voila.
list comp is probably faster (i haven't tested)
wow.
i didnt even know about the filter function. it looks a bit complicated but it works wonders
thanks! :D
|  | Member | | Join Date: Jun 2007
Posts: 100
| | | re: To remove items from a list?
No. That was wrong of me, sorry.
| | Newbie | | Join Date: Apr 2009
Posts: 1
| | | re: To remove items from a list? - >>> l = ['a','b','c','d','e','f','g','h', 'i', 'j','k']
-
>>> l2 = ['d', 'e', 'i']
-
>>> index=0
-
>>> while index<len(l):
-
if l[index] not in l2:
-
l.remove(l[index])
-
else:
-
index= index+1
-
-
-
>>> print l
-
['d', 'e', 'i']
hi ,
this code will give the correct answer.
In your code when you use for loop after removing the item it will increment the index of the list ,because of that it will skip one value and move to next item .
| | Member | | Join Date: Feb 2007
Posts: 32
| | | re: To remove items from a list?
Please correct my understanding. If you get what you want, then as Lee14 shown the output which is same as l2. In that case why do you need to use filter or list comprehension, you can simply replace l by l2...
Am I missing something
SKN
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,565
| | | re: To remove items from a list? Quote:
Originally Posted by hidrkannan Please correct my understanding. If you get what you want, then as Lee14 shown the output which is same as l2. In that case why do you need to use filter or list comprehension, you can simply replace l by l2...
Am I missing something
SKN I think this may be a simplified example or learning exercise. Otherwise you could do something similar to what you suggest.
This effectively assigns a copy of list2 to list1.
-BV
|  | Site Moderator | | Join Date: May 2007 Location: New Hampshire
Posts: 2,572
| | | re: To remove items from a list? Quote:
Originally Posted by micmast in all honesty, that is the trouble I have with python, you have a few millions way to do the same thing :)
anyway, good to learn another new trick :) To tell you the truth, that is one of the reasons that I love languages like Python and Perl (my forte). Having more than one way to achieve the same goal is never a bad thing. It allows for you to get creative, especially when you know how to manipulate the language.
Regards,
Jeff
| | Newbie | | Join Date: Jul 2009
Posts: 3
| | | re: To remove items from a list?
hi all,
I am trying to remove the following from mylist
mylist=[28, 227, 28, 227, 0, 255, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 68, 187, 68, 187, 7, 248, 7, 248, 0, 0, 0, 0, 0, 0, 0, 0, 70, 185, 70, 185, 2, 253, 2, 253]
I only want to remove the zeros that are being repeated every 8th,24th item. I tried a few methods like using filter and all it worked but it also removed the zeros which i intend to keep like the 4th and 6th item in mylist...Is there a good way to do this?...thks
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|