To remove items from a list?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • calred
    New Member
    • Mar 2009
    • 4

    To remove items from a list?

    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:
    Code:
    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?
  • micmast
    New Member
    • Mar 2008
    • 144

    #2
    Tricky problem :)

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

    [CODE=python]
    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)
    [/CODE]

    I know this isn't ideal for a lot of reasons, but that is the best I could come up with

    Comment

    • benjumanji
      New Member
      • Apr 2009
      • 2

      #3
      list comp? filter?

      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)

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        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().

        Comment

        • micmast
          New Member
          • Mar 2008
          • 144

          #5
          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 :)

          Comment

          • calred
            New Member
            • Mar 2009
            • 4

            #6
            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

            Comment

            • Smygis
              New Member
              • Jun 2007
              • 126

              #7
              No. That was wrong of me, sorry.

              Comment

              • Lee14
                New Member
                • Apr 2009
                • 1

                #8
                Code:
                >>> 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 .
                Last edited by bvdet; Apr 23 '09, 02:51 PM. Reason: Add code tags

                Comment

                • hidrkannan
                  New Member
                  • Feb 2007
                  • 32

                  #9
                  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

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    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.

                    Code:
                    list1 = list2[:]
                    This effectively assigns a copy of list2 to list1.

                    -BV

                    Comment

                    • numberwhun
                      Recognized Expert Moderator Specialist
                      • May 2007
                      • 3467

                      #11
                      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

                      Comment

                      • gsr1972
                        New Member
                        • Jul 2009
                        • 3

                        #12
                        removing 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

                        Comment

                        Working...