Home arrow Python arrow Page 4 - Python Sets

Removing Data from A Set - Python

In our last article we left off discussing Python's version of arrays (the list and dictionary). I also gave you a brief introduction to some Operators. In this tutorial I will tell you about Python's remaining data holder, Sets, and prepare you for a later discussion of Operators in Python.

TABLE OF CONTENTS:
  1. Python Sets
  2. Adding to a Set
  3. Copying Sets and Testing for Membership
  4. Removing Data from A Set
  5. Don't Make Me Repeat Myself!
  6. Using Operators on Sets
By: James Payne
Rating: starstarstarstarstar / 11
December 03, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: