Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="typesseq-xrange.html" /> | |
13 | <link rel="parent" href="typesseq.html" /> | |
14 | <link rel="next" href="types-set.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>2.3.6.4 Mutable Sequence Types </title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="2.3.6.3 XRange Type" | |
24 | href="typesseq-xrange.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="2.3.6 Sequence Types " | |
27 | href="typesseq.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="2.3.7 Set Types " | |
30 | href="types-set.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="typesseq-xrange.html">2.3.6.3 XRange Type</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="typesseq.html">2.3.6 Sequence Types </A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="types-set.html">2.3.7 Set Types </A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H3><A NAME="SECTION004364000000000000000"></A><A NAME="typesseq-mutable"></A> | |
55 | <BR> | |
56 | 2.3.6.4 Mutable Sequence Types | |
57 | </H3> | |
58 | ||
59 | <P> | |
60 | List objects support additional operations that allow in-place | |
61 | modification of the object. | |
62 | Other mutable sequence types (when added to the language) should | |
63 | also support these operations. | |
64 | Strings and tuples are immutable sequence types: such objects cannot | |
65 | be modified once created. | |
66 | The following operations are defined on mutable sequence types (where | |
67 | <var>x</var> is an arbitrary object): | |
68 | <a id='l2h-215' xml:id='l2h-215'></a><a id='l2h-216' xml:id='l2h-216'></a> | |
69 | <P> | |
70 | <div class="center"><table class="realtable"> | |
71 | <thead> | |
72 | <tr> | |
73 | <th class="center">Operation</th> | |
74 | <th class="left" >Result</th> | |
75 | <th class="center">Notes</th> | |
76 | </tr> | |
77 | </thead> | |
78 | <tbody> | |
79 | <tr><td class="center" valign="baseline"><code><var>s</var>[<var>i</var>] = <var>x</var></code></td> | |
80 | <td class="left" >item <var>i</var> of <var>s</var> is replaced by <var>x</var></td> | |
81 | <td class="center"></td></tr> | |
82 | <tr><td class="center" valign="baseline"><code><var>s</var>[<var>i</var>:<var>j</var>] = <var>t</var></code></td> | |
83 | <td class="left" >slice of <var>s</var> from <var>i</var> to <var>j</var> is replaced by <var>t</var></td> | |
84 | <td class="center"></td></tr> | |
85 | <tr><td class="center" valign="baseline"><code>del <var>s</var>[<var>i</var>:<var>j</var>]</code></td> | |
86 | <td class="left" >same as <code><var>s</var>[<var>i</var>:<var>j</var>] = []</code></td> | |
87 | <td class="center"></td></tr> | |
88 | <tr><td class="center" valign="baseline"><code><var>s</var>[<var>i</var>:<var>j</var>:<var>k</var>] = <var>t</var></code></td> | |
89 | <td class="left" >the elements of <code><var>s</var>[<var>i</var>:<var>j</var>:<var>k</var>]</code> are replaced by those of <var>t</var></td> | |
90 | <td class="center">(1)</td></tr> | |
91 | <tr><td class="center" valign="baseline"><code>del <var>s</var>[<var>i</var>:<var>j</var>:<var>k</var>]</code></td> | |
92 | <td class="left" >removes the elements of <code><var>s</var>[<var>i</var>:<var>j</var>:<var>k</var>]</code> from the list</td> | |
93 | <td class="center"></td></tr> | |
94 | <tr><td class="center" valign="baseline"><code><var>s</var>.append(<var>x</var>)</code></td> | |
95 | <td class="left" >same as <code><var>s</var>[len(<var>s</var>):len(<var>s</var>)] = [<var>x</var>]</code></td> | |
96 | <td class="center">(2)</td></tr> | |
97 | <tr><td class="center" valign="baseline"><code><var>s</var>.extend(<var>x</var>)</code></td> | |
98 | <td class="left" >same as <code><var>s</var>[len(<var>s</var>):len(<var>s</var>)] = <var>x</var></code></td> | |
99 | <td class="center">(3)</td></tr> | |
100 | <tr><td class="center" valign="baseline"><code><var>s</var>.count(<var>x</var>)</code></td> | |
101 | <td class="left" >return number of <var>i</var>'s for which <code><var>s</var>[<var>i</var>] == <var>x</var></code></td> | |
102 | <td class="center"></td></tr> | |
103 | <tr><td class="center" valign="baseline"><code><var>s</var>.index(<var>x</var><big>[</big>, <var>i</var><big>[</big>, <var>j</var><big>]</big><big>]</big>)</code></td> | |
104 | <td class="left" >return smallest <var>k</var> such that <code><var>s</var>[<var>k</var>] == <var>x</var></code> and | |
105 | <code><var>i</var> <= <var>k</var> < <var>j</var></code></td> | |
106 | <td class="center">(4)</td></tr> | |
107 | <tr><td class="center" valign="baseline"><code><var>s</var>.insert(<var>i</var>, <var>x</var>)</code></td> | |
108 | <td class="left" >same as <code><var>s</var>[<var>i</var>:<var>i</var>] = [<var>x</var>]</code></td> | |
109 | <td class="center">(5)</td></tr> | |
110 | <tr><td class="center" valign="baseline"><code><var>s</var>.pop(<big>[</big><var>i</var><big>]</big>)</code></td> | |
111 | <td class="left" >same as <code><var>x</var> = <var>s</var>[<var>i</var>]; del <var>s</var>[<var>i</var>]; return <var>x</var></code></td> | |
112 | <td class="center">(6)</td></tr> | |
113 | <tr><td class="center" valign="baseline"><code><var>s</var>.remove(<var>x</var>)</code></td> | |
114 | <td class="left" >same as <code>del <var>s</var>[<var>s</var>.index(<var>x</var>)]</code></td> | |
115 | <td class="center">(4)</td></tr> | |
116 | <tr><td class="center" valign="baseline"><code><var>s</var>.reverse()</code></td> | |
117 | <td class="left" >reverses the items of <var>s</var> in place</td> | |
118 | <td class="center">(7)</td></tr> | |
119 | <tr><td class="center" valign="baseline"><code><var>s</var>.sort(<big>[</big><var>cmp</var><big>[</big>, | |
120 | <var>key</var><big>[</big>, <var>reverse</var><big>]</big><big>]</big><big>]</big>)</code></td> | |
121 | <td class="left" >sort the items of <var>s</var> in place</td> | |
122 | <td class="center">(7), (8), (9), (10)</td></tr></tbody> | |
123 | </table></div> | |
124 | <a id='l2h-217' xml:id='l2h-217'></a><a id='l2h-218' xml:id='l2h-218'></a><a id='l2h-219' xml:id='l2h-219'></a><a id='l2h-220' xml:id='l2h-220'></a><a id='l2h-221' xml:id='l2h-221'></a><a id='l2h-222' xml:id='l2h-222'></a><a id='l2h-223' xml:id='l2h-223'></a><a id='l2h-225' xml:id='l2h-225'></a> | |
125 | Notes: | |
126 | <DL> | |
127 | <DT><STRONG>(1)</STRONG></DT> | |
128 | <DD><var>t</var> must have the same length as the slice it is | |
129 | replacing. | |
130 | ||
131 | <P> | |
132 | </DD> | |
133 | <DT><STRONG>(2)</STRONG></DT> | |
134 | <DD>The C implementation of Python has historically accepted | |
135 | multiple parameters and implicitly joined them into a tuple; this | |
136 | no longer works in Python 2.0. Use of this misfeature has been | |
137 | deprecated since Python 1.4. | |
138 | ||
139 | <P> | |
140 | </DD> | |
141 | <DT><STRONG>(3)</STRONG></DT> | |
142 | <DD><var>x</var> can be any iterable object. | |
143 | ||
144 | <P> | |
145 | </DD> | |
146 | <DT><STRONG>(4)</STRONG></DT> | |
147 | <DD>Raises <tt class="exception">ValueError</tt> when <var>x</var> is not found in | |
148 | <var>s</var>. When a negative index is passed as the second or third parameter | |
149 | to the <tt class="method">index()</tt> method, the list length is added, as for slice | |
150 | indices. If it is still negative, it is truncated to zero, as for | |
151 | slice indices. | |
152 | <span class="versionnote">Changed in version 2.3: | |
153 | Previously, <tt class="method">index()</tt> didn't | |
154 | have arguments for specifying start and stop positions.</span> | |
155 | ||
156 | <P> | |
157 | </DD> | |
158 | <DT><STRONG>(5)</STRONG></DT> | |
159 | <DD>When a negative index is passed as the first parameter to | |
160 | the <tt class="method">insert()</tt> method, the list length is added, as for slice | |
161 | indices. If it is still negative, it is truncated to zero, as for | |
162 | slice indices. | |
163 | <span class="versionnote">Changed in version 2.3: | |
164 | Previously, all negative indices | |
165 | were truncated to zero.</span> | |
166 | ||
167 | <P> | |
168 | </DD> | |
169 | <DT><STRONG>(6)</STRONG></DT> | |
170 | <DD>The <tt class="method">pop()</tt> method is only supported by the list and | |
171 | array types. The optional argument <var>i</var> defaults to <code>-1</code>, | |
172 | so that by default the last item is removed and returned. | |
173 | ||
174 | <P> | |
175 | </DD> | |
176 | <DT><STRONG>(7)</STRONG></DT> | |
177 | <DD>The <tt class="method">sort()</tt> and <tt class="method">reverse()</tt> methods modify the | |
178 | list in place for economy of space when sorting or reversing a large | |
179 | list. To remind you that they operate by side effect, they don't return | |
180 | the sorted or reversed list. | |
181 | ||
182 | <P> | |
183 | </DD> | |
184 | <DT><STRONG>(8)</STRONG></DT> | |
185 | <DD>The <tt class="method">sort()</tt> method takes optional arguments for | |
186 | controlling the comparisons. | |
187 | ||
188 | <P> | |
189 | <var>cmp</var> specifies a custom comparison function of two arguments | |
190 | (list items) which should return a negative, zero or positive number | |
191 | depending on whether the first argument is considered smaller than, | |
192 | equal to, or larger than the second argument: | |
193 | "<tt class="samp"><var>cmp</var>=<tt class="keyword">lambda</tt> <var>x</var>,<var>y</var>: | |
194 | <tt class="function">cmp</tt>(x.lower(), y.lower())</tt>" | |
195 | <P> | |
196 | <var>key</var> specifies a function of one argument that is used to | |
197 | extract a comparison key from each list element: | |
198 | "<tt class="samp"><var>key</var>=<tt class="function">str.lower</tt></tt>" | |
199 | <P> | |
200 | <var>reverse</var> is a boolean value. If set to <code>True</code>, then the | |
201 | list elements are sorted as if each comparison were reversed. | |
202 | ||
203 | <P> | |
204 | In general, the <var>key</var> and <var>reverse</var> conversion processes are | |
205 | much faster than specifying an equivalent <var>cmp</var> function. This is | |
206 | because <var>cmp</var> is called multiple times for each list element while | |
207 | <var>key</var> and <var>reverse</var> touch each element only once. | |
208 | ||
209 | <P> | |
210 | ||
211 | <span class="versionnote">Changed in version 2.3: | |
212 | Support for <code>None</code> as an equivalent to omitting | |
213 | <var>cmp</var> was added.</span> | |
214 | ||
215 | <P> | |
216 | ||
217 | <span class="versionnote">Changed in version 2.4: | |
218 | Support for <var>key</var> and <var>reverse</var> was added.</span> | |
219 | ||
220 | <P> | |
221 | </DD> | |
222 | <DT><STRONG>(9)</STRONG></DT> | |
223 | <DD>Starting with Python 2.3, the <tt class="method">sort()</tt> method is | |
224 | guaranteed to be stable. A sort is stable if it guarantees not to | |
225 | change the relative order of elements that compare equal -- this is | |
226 | helpful for sorting in multiple passes (for example, sort by | |
227 | department, then by salary grade). | |
228 | ||
229 | <P> | |
230 | </DD> | |
231 | <DT><STRONG>(10)</STRONG></DT> | |
232 | <DD>While a list is being sorted, the effect of attempting to | |
233 | mutate, or even inspect, the list is undefined. The C | |
234 | implementation of Python 2.3 and newer makes the list appear empty | |
235 | for the duration, and raises <tt class="exception">ValueError</tt> if it can detect | |
236 | that the list has been mutated during a sort. | |
237 | </DD> | |
238 | </DL> | |
239 | ||
240 | <P> | |
241 | ||
242 | <DIV CLASS="navigation"> | |
243 | <div class='online-navigation'> | |
244 | <p></p><hr /> | |
245 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
246 | <tr> | |
247 | <td class='online-navigation'><a rel="prev" title="2.3.6.3 XRange Type" | |
248 | href="typesseq-xrange.html"><img src='../icons/previous.png' | |
249 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
250 | <td class='online-navigation'><a rel="parent" title="2.3.6 Sequence Types " | |
251 | href="typesseq.html"><img src='../icons/up.png' | |
252 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
253 | <td class='online-navigation'><a rel="next" title="2.3.7 Set Types " | |
254 | href="types-set.html"><img src='../icons/next.png' | |
255 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
256 | <td align="center" width="100%">Python Library Reference</td> | |
257 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
258 | href="contents.html"><img src='../icons/contents.png' | |
259 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
260 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
261 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
262 | <td class='online-navigation'><a rel="index" title="Index" | |
263 | href="genindex.html"><img src='../icons/index.png' | |
264 | border='0' height='32' alt='Index' width='32' /></A></td> | |
265 | </tr></table> | |
266 | <div class='online-navigation'> | |
267 | <b class="navlabel">Previous:</b> | |
268 | <a class="sectref" rel="prev" href="typesseq-xrange.html">2.3.6.3 XRange Type</A> | |
269 | <b class="navlabel">Up:</b> | |
270 | <a class="sectref" rel="parent" href="typesseq.html">2.3.6 Sequence Types </A> | |
271 | <b class="navlabel">Next:</b> | |
272 | <a class="sectref" rel="next" href="types-set.html">2.3.7 Set Types </A> | |
273 | </div> | |
274 | </div> | |
275 | <hr /> | |
276 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
277 | </DIV> | |
278 | <!--End of Navigation Panel--> | |
279 | <ADDRESS> | |
280 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
281 | </ADDRESS> | |
282 | </BODY> | |
283 | </HTML> |