Home arrow Python arrow Page 2 - Python Sets

Adding to 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.

  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



Let's say the Burger King wasn't having things his way and decided to quit and join the McDonald's gang. After a rough initiation (he gunned down Jack from Jack in the Box), he is allowed in. Since Ronald is a demanding boss, we want to add him to our Set. We could do so this way:


mcdonaldgang = Set (['Grimace', 'Hamburglar', 'Mayor Mccheese'])

print mcdonaldgang

mcdonaldgang.add('Burger King')

print mcdonaldgang

The above would first print out our original set, then add the Burger King to it, and print it once more:

  Hamburglar, Grimace, Mayor Mccheese

  Hamburglar Burger King Grimace Mayor Mccheese

Let's say not only the Burger King, but Wendy herself wanted to join the gang. To add more than one new element at the same time, we would use the Update function:


mcdonaldgang = Set (['Grimace', 'Hamburglar', 'Mayor Mccheese'])

print mcdonaldgang

mcdonaldgang.update(['Burger King', 'Wendy'])

print mcdonaldgang

Again, we set the initial values for the set mcdonaldgang, printed those values, then used the update function to add two more elements to the set. The resulting print out would be:

  Grimace Mayor Mccheese Hamburglar

  Hamburglar Mayor Mccheese Grimace Wendy Burger King

Again note that Sets do not allow for duplicate values. Let's say the Fry Guys all wanted to join the McDonald's Gang. Let's add them to our database and see what happens.


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

print mcdonaldgang

mcdonaldgang.update(['Fry Guy', 'Fry Guy', 'Fry Guy'])

print mcdonaldgang

This will result in the print out:

  Grimace Mayor Mccheese Hamburglar Burger King Wendy

  Fry Guy Grimace Mayor Mccheese Hamburglar Burger King Wendy

As you can see it only adds the one Fry Guy. Too bad for the rest of them. That's what you get for all having the same name.

>>> More Python Articles          >>> More By James Payne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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


Dev Shed Tutorial Topics: