8 Replies - 6712 Views - Last Post: 07 March 2010 - 01:03 PM Rate Topic: -----

#1 cnampheonix  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 03-December 09

Remove list b from list a - Python

Posted 07 March 2010 - 10:18 AM

Im having a little issue here where I asked input from user to make a list (list a)
And if anything from list a falls into list b (list b is global list I made)
then that stuff would be removed from list b and
then print "you still have " ,list b

My problem is that its only removing the first element of list a from list b
And when I print out what you still have left it comes out like this
You still have 1 , 2 , 3 , 4 etc.. (just an example)
Rather than
You still have 1
You still have 2
etc..

I tried a couple things but i just get errors, kinda stuck, i know there is definitely something wrong with my for loop and the way i put in my lists but i dunno how to fix it.
for list_a in list_b:
    list_b.remove(list_a)
print "you still have", list_a


This post has been edited by cnampheonix: 07 March 2010 - 10:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Remove list b from list a - Python

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Remove list b from list a - Python

Posted 07 March 2010 - 10:41 AM

Your issue is that you get an error if the list does not contain an element.
For example,

list_a = [2,4,7]
list_b = [1,2,3,4,5,6]

list_b does not contain all the elements in list_a (missing 7), so if you try to remove it, you will get a ValueError. What you have to do is to count the number of times that that element is found in list_a. Also, the replace method only replaces the first instance and leaves all others behind. You also need to loop while the count of that numbers does not equal 0. You can do this easily using the count() function and a nested while loop. (count() counts the number of times that a value is found)

for item in list_a:
    while list_b.count(item) > 0:
        list_b.remove(item)



:)
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: Remove list b from list a - Python

Posted 07 March 2010 - 11:01 AM

"Remove list b from list a"

The easiest way to do this is probably to make a new list_a.

>>> list_a = [2,4,7]
>>> list_b = [1,2,3,4,5,6]
>>> list_a = [n for n in list_a if n not in list_b]
>>> list_a
[7]
>>>


Was This Post Helpful? 1
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Remove list b from list a - Python

Posted 07 March 2010 - 11:07 AM

Wow. that's beautiful baavgai. Makes mine look crude :P
Was This Post Helpful? 0
  • +
  • -

#5 cnampheonix  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 03-December 09

Re: Remove list b from list a - Python

Posted 07 March 2010 - 12:34 PM

Im still having problems trying to remove elements in the list. :online2long:
heres what I got
def core_a(COURSE):
    print
    print "Part A requirements"
    for num in COURSE:
        A_LIST.remove(num)
    if len(A_LIST) > 0:
        print "You need to take", num
    else:
        print "You satisfied the Part A Requirements"



I know that A_LIST.remove(num) is wrong, that was just the last thing I did.
I tried doing your method Dogstopper and Baavgai but it wouldnt work
it gave me an error of saying A_LIST was referenced before assignment
And it also told me that the list.remove(x): x not in list
I think im not understanding something...

This post has been edited by cnampheonix: 07 March 2010 - 12:35 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Remove list b from list a - Python

Posted 07 March 2010 - 12:39 PM

It doesn't seem that you read the explanations. You need to have list_a and list_b to firstly have values...Secondly, you have to check whether num exists before removing it (You can't remove nothing).

# Make sure you initialize A_LIST  and B_LIST up here 

def core_a(COURSE):
    print
    print "Part A requirements"

    for num in COURSE:
        # Remove all instances of num IF they exist
        while A_LIST.count(num) > 0:
            A_LIST.remove(num)

    if len(A_LIST) > 0:
        print "You need to take", num
    else:
        print "You satisfied the Part A Requirements"



Mine and baavgai's examples both work. Try running them!
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: Remove list b from list a - Python

Posted 07 March 2010 - 12:47 PM

# is COURSE a single item or a list?
# I bet it's just an item...
def core_a(COURSE):
    print
    print "Part A requirements"
    print "Course taken ", COURSE

    # the for makes not sense here
    # if COURSE is a single value, it makes less sense
    # for num in COURSE:

    # how about
    if COURSE in A_LIST:
        A_LIST.remove(COURSE)

    # now you've removed it if it's there
    # check your list
    if len(A_LIST) > 0:
        # num doesn't mean anything here
        # don't you want to show the contents of the list?
        # print "You need to take", num

        # loop through your list
        for num in A_LIST: 
            # now num has something meaningful
            print "You need to take", num
    else:
        print "You satisfied the Part A Requirements"


Was This Post Helpful? 0
  • +
  • -

#8 cnampheonix  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 03-December 09

Re: Remove list b from list a - Python

Posted 07 March 2010 - 12:50 PM

Right, my apologies for being really thickheaded with this.
So I had A_LIST initialized at the top as a global
Also COURSE is a list that is created from input
I initialized that at the top with COURSE = []
afterwhich in def main() courses would be put into it.
so I tried what dogstopper said
i had
A_LIST = (alot of courses like "CMSC 201", "etc.. "
And just to make a quick check I had COURSE = [CMSC 201]
so CMSC 201 should have been removed from A_LIST
but when i printed all i got was
You need to take CMSC 201

This post has been edited by cnampheonix: 07 March 2010 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Remove list b from list a - Python

Posted 07 March 2010 - 01:03 PM

Both need to have the same TYPE of variables...
A_LIST = (alot of courses like "CMSC 201", "etc.. "

So that means that A_LIST contains a load of strings.
So you need
COURSE = ["CMSC 201"]

to make that a string list too.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1