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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 8
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT