yet another ridiculously simple question, yet i feel compelled to ask because i've been stuck here for like an hour without it working

ok so say you have one list, a, and you need the user of the program to input a list of several more numbers, call that list b, then take out the numbers in b from a.

``````>>>a = [1,2,3,4,5,6,7,8,9,10,10,10]
>>>b = input ("Enter numbers: ")
[3,4,10]
>>>#something here
>>>print (a)
[1,2,5,6,7,8,9,10,10]``````

so i try a bunch of stuff from the tutorial in like 100 different ways: a = a - b, for i in b: a.remove(i), a.remove(b)... yet i always get an error

it works with sets but i can't use sets because they keep throwing out the nonunique numbers (so like if you have [10,10,10] it would just make it [10])

## All 9 Replies

``````for number in b:
if number in a:
a.remove(number)``````

That should go through each number in b and remove it from a if it appears in a...

Use simple boolean statements.
Here is my python interaction:

``````>>> a = [1,2,3,4,5,6,7,8,9,10,10,10]
>>> b = list(input ("Enter numbers: "))
Enter numbers: [3,4,10]
>>> a= [x for x in a if x not in b]
>>> a
[1, 2, 5, 6, 7, 8, 9]
>>>``````

siddhant3s, you may notice that your option has in fact removed the three 10s from list a, when you only input one 10. In the question, the other two 10s are left in list a after only one 10 is input.

shadwick man's version works great, but i think my real problem is inputting the list

if the list is already defined it works great

``````>>>a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10]
>>>b = [3,4,10]
>>>for number in b:
>>>    if number in a:
>>>        a.remove(number)
>>>print (a)
[1,2,5,6,7,8,9,10,10]``````

but i can't find a way for the user to input it

``````>>>a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10]
>>>b = input("enter numbers: ")
[3,4,10]
b= list(b) #i would think this should convert it from string to list but it doesn't
>>>for number in b:
>>>    if number in a:
>>>        a.remove(number)
>>>print (a)
[1,2,5,6,7,8,9,10,10]``````

if i say print (b) it does not define b as [3,4] but rather as '[3, 4]'

If you want, you can have it take input and split it at every space, or comma, etc:

``b = raw_input("Enter numbers separated by a space: ").split(" ")``

It works like this:

``````>>>Enter numbers separated by a space: 3 4 10
>>>b
>>>[3,4,10]``````

that's exactly what i'm looking for shadwickman... but when i do it it doesn't work...

``b = raw_input("Enter numbers separated by a space: ").split(" ")``

what i get is

``````Enter numbers separated by a space: 3 4 5
>>> b
['3', '4', '5']``````

it does not see the numbers as integers for some reason... and it does not remove them from list "a" either

Oh wow, I'm sorry. I completely forgot about raw_input returning them as strings. You can run each index through the int() function, changing it to an integer, but a problem arises if someone enters something other than digits. Let me give two solutions...

This one is simple, yet throws an error if the user enters any non-digit characters.

``````b = raw_input("Enter numbers separated by a space: ").split(" ")
for i, n in enumerate(b):
b[i] = int(n)

# can also be written as:
# b = [int(i) for i in raw_input("Enter numbers separated by a space: ").split(" ")]``````

This one loops if there are any non-digits, and handles the error they present.

``````while 1:
end = True  # end loop unless error
b = raw_input("Enter numbers separated by a space: ").split(" ")
for i, n in enumerate(b):
try:
b[i] = int(n)  # will cause error if i can't be converted to int
except:
print "Non-numerical input found. Only enter numbers!"
end = False
if end:
break``````

thanks, that first one works perfectly!

not sure how it works but i need digit only so its perfect.

All it does is split the input into a list and save it as b like we did before. As of then, we have a list of numbers, but they are actually strings (e.g. ).

From there, we use a for loop to cycle though list b (i is the index number, and n is the index value from the enumerate function). All we do then is tell it to replace b (the current index it's on) with the old value of it (the string, n) converted to an integer through the int function.

I hope that clarifies it a bit!

commented: really helpful, thanks a lot +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.