Python Sets - Copying Sets and Testing for Membership
(Page 3 of 6 )
If you want to copy a Set, you can do so pretty simply:
/usr/local/bin/python
mcdonaldgang = Set (['Grimace', 'Hamburglar', 'Mayor Mccheese', 'Burger King', 'Wendy', 'Fry Guy'])
gang = mcdonaldgang.copy()
print gang
Here would be the result of this program:
Fry Guy Grimace Mayor Mccheese Hamburglar Burger King Wendy
Suppose the gang had grown so large Ronald couldn't keep track of it in his head. To see if someone was in his gang, he could use the In operator.
/usr/local/bin/python
mcdonaldgang = Set (['Grimace', 'Hamburglar', 'Mayor Mccheese', 'Burger King', 'Wendy', 'Fry Guy'])
'Grimace' in mcdonaldgang
'Birdie' in mcdonaldgang
This program checks to see if Grimace and Birdie are in the McDonald's Gang. Here is the result:
True
False
Meaning that Grimace is in the gang, but Birdie is not. Who cares...she was annoying anyway. And now you know where those chicken nuggets come from.
Next: Removing Data from A Set >>
More Python Articles
More By James Payne