Python
  Home arrow Python arrow Page 4 - Python Sets
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python Sets
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2007-12-03

    Table of Contents:
  • Python Sets
  • Adding to a Set
  • Copying Sets and Testing for Membership
  • Removing Data from A Set
  • Don't Make Me Repeat Myself!
  • Using Operators on Sets

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    Python Sets - Removing Data from A Set


    (Page 4 of 6 )

    As happens in gangs, sometimes members disappear. Let's say that Colonel Sanders has had enough of the McDonald's Gang encroaching on his turf. Se he sets loose the hound from hell, aka the Taco Bell dog. The end result is the demise of the Hamburglar.

      Rubble Rubble.

    There are three ways Ronald can remove the Hamburglar from the Set. Here they are.

    Pop Goes the Greaseball

    The first method for deleting an element from a Set is the pop. It simply removes an element from the set.


    /usr/local/bin/python


    mcdonaldgang = Set (['Grimace', 'Hamburglar', 'Mayor Mccheese', 'Burger King', 'Wendy', 'Fry Guy'])


    mcdonaldgang.pop()

    The pop method may or may not remove the Hamburglar. This is because the pop method knows no rules. It deletes who it wants, when it wants. It picks an element at random, and deletes them. Period.

    If you want to specifically remove the Hamburglar, you might want to try out Pop's more lawful cousin, the Remove function:


    /usr/local/bin/python


    mcdonaldgang = Set (['Grimace', 'Hamburglar', 'Mayor Mccheese', 'Burger King', 'Wendy', 'Fry Guy'])


    mcdonaldgang.remove('Hamburglar')

    print mcdonaldgang

    The Remove function will remove the element that you specify. When we print out our list it will be:

      Grimace Wendy Mayor Mccheese Fry Guy Burger King

    The only problem with the Remove function is that if you make a typo or the value does not exist in the Set, an error will be returned. To overcome this problem, simply use our third function, the Discard function.


    /usr/local/bin/python


    mcdonaldgang = Set (['Grimace', 'Hamburglar', 'Mayor Mccheese', 'Burger King', 'Wendy', 'Fry Guy'])


    mcdonaldgang.discard('Hamburglar')

    print mcdonaldgang

    This gives us the same result:

      Grimace Wendy Mayor Mccheese Fry Guy Burger King

    Lastly, if we wanted to remove all of the data in a Set, we can use the Clear function.


    usr/local/bin/python


    mcdonaldgang = Set (['Grimace', 'Hamburglar', 'Mayor Mccheese', 'Burger King', 'Wendy', 'Fry Guy'])


    mcdonaldgang.clear()

    More Python Articles
    More By James Payne


       · I am curious, Why didn't the author use the Python shell for the tutorials?-...
       · Thanks for dropping by to read my article. In this issue we discuss Sets in Python;...
       · The tutorial didn't work for me.I got a message saying that "Set" was not...
       · I tested it on my end and it worked. Let me look into it a little more and get back...
       · Thanks James,Here is a bit more information for you. Hope it is useful.The error...
       · The reason it doesn't work is because the name of the function to create a set is...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway