[Tutor] Using the set.difference method with an unknown number of input iterables

Emile van Sebille emile at fenx.com
Fri Oct 19 07:52:52 CEST 2012


On 10/18/2012 10:38 AM, Ryan Waples wrote:> I'm struggling to understand 
how to understand/accomplish the following:
 >
 > I have an set ("a" below) and a list of sets ("not_a"), how can I pass
 > the elements of "not_a" to set.difference() so that it it understands I
 > want the difference between set "a" and all the rest
 >
 > set.difference says "Changed in version 2.6: Accepts multiple input
 > iterables".
 > How can I give it multiple input iterables?
 >
 > I get different error msgs depending on what I try, but they just tell
 > me that there is something that I'm missing here.
 >
 > Thanks
 >
 > #Code below
 > a = set([1,2,3,4])
 > b = set([2,3,4,5])
 > c = set([3,4,5,6])
 > d = set([4,5,6,7])
 >
 > not_a = [b,c,d]
 > a.difference(not_a)

Try this as

a.difference(*not_a)

The '*' expands the list to its individual items.

HTH,

Emile


 >
 > # I expect to return set([1]), the same as if I called:
 > a.difference(b,c,d)
 >
 >
 >
 >
 > _______________________________________________
 > Tutor maillist  -  Tutor at python.org
 > To unsubscribe or change subscription options:
 > http://mail.python.org/mailman/listinfo/tutor
 >




More information about the Tutor mailing list