By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,402 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,402 IT Pros & Developers. It's quick & easy.

To remove items from a list?

P: 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:
Expand|Select|Wrap|Line Numbers
  1. l = ['a','b','c','d','e','f','g','h', 'i', 'j','k']
  2. l2 = ['d', 'e', 'i']    # l2 could be a list of raw inputs
  4. for x in l:
  5.     if x not in r:
  6.         l.remove(x)
  7. 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?
Apr 1 '09 #1
Share this Question
Share on Google+
11 Replies

P: 144
Tricky problem :)

After trying a while, before giving up I though about using 2 identical lists.

Expand|Select|Wrap|Line Numbers
  1. l = ['a','b','c','d','e','f','g']
  2. l2 = ['a','b']
  3. l3 = ['a','b','c','d','e','f','g']
  4. for x in l:
  5.  if x not in l2:
  6.   l3.remove(x)
I know this isn't ideal for a lot of reasons, but that is the best I could come up with
Apr 1 '09 #2

P: 2
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)


[p for p in x if p not in y]

et voila.

list comp is probably faster (i haven't tested)
Apr 1 '09 #3

Expert Mod 2.5K+
P: 2,851
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().
Apr 1 '09 #4

P: 144
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 :)
Apr 1 '09 #5

P: 4

i didnt even know about the filter function. it looks a bit complicated but it works wonders
thanks! :D
Apr 1 '09 #6

P: 126
No. That was wrong of me, sorry.
Apr 6 '09 #7

P: 1
Expand|Select|Wrap|Line Numbers
  1. >>> l = ['a','b','c','d','e','f','g','h', 'i', 'j','k']
  2. >>> l2 = ['d', 'e', 'i']
  3. >>> index=0
  4. >>> while index<len(l):
  5.     if l[index] not in l2:
  6.         l.remove(l[index])
  7.     else:
  8.         index= index+1
  11. >>> print l
  12. ['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 .
Apr 23 '09 #8

P: 32
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

Jun 1 '09 #9

Expert Mod 2.5K+
P: 2,851
I think this may be a simplified example or learning exercise. Otherwise you could do something similar to what you suggest.

Expand|Select|Wrap|Line Numbers
  1. list1 = list2[:]
This effectively assigns a copy of list2 to list1.

Jun 1 '09 #10

Expert Mod 2.5K+
P: 3,503
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.


Jun 21 '09 #11

P: 3
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
Jul 3 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.