Home arrow Python arrow Page 3 - Sequences and Sets in Python

Set Operations - Python

In this fifth part of a nine-part series on the Python language, you'll learn about sequences, lists, sets, and more. This article is excerpted from chapter four of the book Python in a Nutshell, Second Edition, written by Alex Martelli (O'Reilly; ISBN: 0596100469). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

TABLE OF CONTENTS:
  1. Sequences and Sets in Python
  2. List methods
  3. Set Operations
  4. Indexing a Dictionary
By: O'Reilly Media
Rating: starstarstarstarstar / 3
October 09, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Python provides a variety of operations applicable to sets. Since sets are containers, the built-in len function can take a set as its single argument and return the number of items in the set object. A set is iterable, so you can pass it to any function or method that takes an iterable argument. In this case, the items of the set are iterated upon, in some arbitrary order. For example, for any set S , min(S) returns the smallest item in S .

Set Membership

The k  in S  operator checks whether object k is one of the items of set S . It returns True if it is and False if it isn't. Similarly, not in S  is just like not (k in S).

Set Methods

Set objects provide several methods, as shown in Table 4-4. Nonmutating methods return a result without altering the object to which they apply and can also be called on instances of type frozenset, while mutating methods may alter the object to which they apply and can be called only on instances of type set. In Table 4-4, S  and S1  indicate any set object, and x any hashable object.

Table 4-4. Set object methods

Method

Description

Nonmutating methods

 

S.copy( )

Returns a shallow copy of the set (a copy whose items are the same objects as Sís, not copies thereof)

S.difference(S1)

Returns the set of all items of Sthat arenít in S1

Table 4-4. Set object methods (continued)

Method

Description

S.intersection(S1)

Returns the set of all items of Sthat are also in S1

S.issubset(S1)

Returns Trueif all items of S are also in S1; otherwise, returns False

S.issuperset(S1)

Returns Trueif all items of S1 are also in S; otherwise, returns False (like S1.issubset(S))

S.symmetric_difference(S1)

Returns the set of all items that are in either Sor S1, but not in both sets

S.union(S1)

Returns the set of all items that are in S, S1, or in both sets

Mutating methods

 

S.add(x)

Adds xas an item to S; no effect if xwas already an item in S

S.clear( )

Removes all items from S, leaving Sempty

S.discard(x)

Removes xas an item of S; no effect if xwas not an item of S

S.pop( )

Removes and returns an arbitrary item of S

S.remove(x)

Removes x as an item of S; raises a KeyErrorexception if x is not an item of S

All mutating methods of set objects, except pop, return None.

The pop method can be used for destructive iteration on a set, consuming little extra memory. The memory savings make pop usable for a loop on a huge set, when what you want is to "consume" the set in the course of the loop.

Sets also have mutating methods named difference_update, intersection_update, symmetric_difference_update, and update (corresponding to nonmutating method union). Each such mutating method performs the same operation as the corresponding nonmutating method, but it performs the operation in place, altering the set on which you call it, and returns None. These four nonmutating methods are also accessible with operator syntax: respectively, S-S1, S&S1, S^S1, and S|S1; the corresponding mutating methods are also accessible with augmented assignment syntax: respectively,
S-=S1, S&=S1, S^=S1, and S|=S1. When you use operator or augmented assignment syntax, both operands must be sets or frozensets; however, when you call the named methods, argument S1  can be any iterable with hashable items, and the semantics are just the same as if the argument you passed was
set(S1).

Dictionary Operations

Python provides a variety of operations applicable to dictionaries. Since dictionaries are containers, the built-in len function can take a dictionary as its single argument and return the number of items (key/value pairs) in the dictionary object. A dictionary is iterable, so you can pass it to any function or method that takes an iterable argument. In this case, only the keys of the dictionary are iterated upon, in some arbitrary order. For example, for any dictionary D , min(D) returns the smallest key in D .

Dictionary Membership

The k  in D  operator checks whether object k  is one of the keys of the dictionary D . It returns True if it is and False if it isn't. k not in D  is just like
not (k in D ).



 
 
>>> More Python Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: