I'm quite new to python and am trying to make a kind of 20Q thing, but am struggling - anybody know how to delete a list from a set, or if not a list from a list?

Thanks :)

From the Library Reference:

The set type is mutable — the contents can be changed using methods like add() and remove().

>>> s = set(range(10))
>>> s
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> s -= set([3,4,5])
>>> s
set([0, 1, 2, 6, 7, 8, 9])
>>> s -= set(range(3,10))
>>> s
set([0, 1, 2])

Thanks, but am trying to remove an entire list from a set , like doing the following:

list1=[1,2,3,4,5,6]
list2=[2,5,6]
a=set(list1)

del(list2) from list1

or something like

del(list2) from list1

I realize these aren't correct lines of code and won't work, but it's this kind of concept i'm trying to acheive. I my actual lists I have so many elements that deleting each element just isn't plausable.

Cheers:)

Thanks, but am trying to remove an entire list from a set , like doing the following:

list1=[1,2,3,4,5,6]
list2=[2,5,6]
a=set(list1)

del(list2) from a

or something like

del(list2) from list1

I realize these aren't correct lines of code and won't work, but it's this kind of concept i'm trying to acheive. I my actual lists I have so many elements that deleting each element just isn't plausable.

Cheers:)

My previous post does remove a whole list from a set. You can do

a = set(list1) - set(list2)

or

a = set(list1)
a -= set(list2) # notice the minus sign

If you want to preserve the order of the elements, you can use

s2 = set(list2)
a = [x for x in list1 if x not in s2]

or

import itertools as itt

s2 = set(list2)
a = list(itt.ifilterfalse(lambda x: x in s2, list1))
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.