Python
  Home arrow Python arrow Page 3 - Sequences and Sets in Python
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? 
Google.com  
PYTHON

Sequences and Sets in Python
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-10-09


    Table of Contents:
  • Sequences and Sets in Python
  • List methods
  • Set Operations
  • Indexing a Dictionary

  • 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


    Sequences and Sets in Python - Set Operations
    ( Page 3 of 4 )

    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
     

       

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek