<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"whatsnew24.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"whatsnew24.html" title='What's New in Python
2.4'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"node3.html" />
<link rel=
"prev" href=
"contents.html" />
<link rel=
"parent" href=
"whatsnew24.html" />
<link rel=
"next" href=
"node3.html" />
<meta name='aesop' content='information'
/>
<title>1 PEP
218: Built-In Set Objects
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"Contents"
href=
"contents.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"What's New in Python"
href=
"whatsnew24.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"2 PEP 237: Unifying"
href=
"node3.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">What's New in Python
2.4</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"contents.html">Contents
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"whatsnew24.html">What's New in Python
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node3.html">2 PEP
237: Unifying
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION000200000000000000000">
1 PEP
218: Built-In Set Objects
</A>
Python
2.3 introduced the
<tt class=
"module">sets
</tt> module. C implementations of
set data types have now been added to the Python core as two new
built-in types,
<tt class=
"function">set(
<var>iterable
</var>)
</tt> and
<tt class=
"function">frozenset(
<var>iterable
</var>)
</tt>. They provide high speed
operations for membership testing, for eliminating duplicates from
sequences, and for mathematical operations like unions, intersections,
differences, and symmetric differences.
<div class=
"verbatim"><pre>
>>> a = set('abracadabra') # form a set from a string
>>> 'z' in a # fast membership testing
>>> a # unique letters in a
set(['a', 'r', 'b', 'c', 'd'])
>>> ''.join(a) # convert back into a string
>>> b = set('alacazam') # form a second set
>>> a - b # letters in a but not in b
>>> a | b # letters in either a or b
set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
>>> a
& b # letters in both a and b
>>> a ^ b # letters in a or b but not both
set(['r', 'd', 'b', 'm', 'z', 'l'])
>>> a.add('z') # add a new element
>>> a.update('wxy') # add multiple new elements
set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z'])
>>> a.remove('x') # take one element out
set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z'])
The
<tt class=
"function">frozenset
</tt> type is an immutable version of
<tt class=
"function">set
</tt>.
Since it is immutable and hashable, it may be used as a dictionary key or
as a member of another set.
The
<tt class=
"module">sets
</tt> module remains in the standard library, and may be
useful if you wish to subclass the
<tt class=
"class">Set
</tt> or
<tt class=
"class">ImmutableSet
</tt>
classes. There are currently no plans to deprecate the module.
<p class=
"heading">See Also:
</p>
<dl compact=
"compact" class=
"seerfc">
<dt><a href=
"http://www.python.org/peps/pep-0218.html"
title=
"Adding a Built-In Set Object Type"
>PEP
218,
<em>Adding a Built-In Set Object Type
</em></a>
<dd>Originally proposed by
Greg Wilson and ultimately implemented by Raymond Hettinger.
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"Contents"
href=
"contents.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"What's New in Python"
href=
"whatsnew24.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"2 PEP 237: Unifying"
href=
"node3.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">What's New in Python
2.4</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"contents.html">Contents
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"whatsnew24.html">What's New in Python
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node3.html">2 PEP
237: Unifying
</A>
<span class=
"release-info">Release
1.01.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.