Team Ai
Apppublic

parthtamu/rag-code-assistant

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
array.html700 linesDownload Raw Back to docs
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4  <head>5    <meta charset="utf-8" />6    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="array — Efficient arrays of numeric values" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/array.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="This module defines an object type which can compactly represent an array of basic values: characters, integers, floating-point numbers. Arrays are mutable sequence types and behave very much like ..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_array_2228e7ad.png" />15<meta property="og:image:alt" content="This module defines an object type which can compactly represent an array of basic values: characters, integers, floating-point numbers. Arrays are mutable sequence types and behave very much like ..." />16<meta name="description" content="This module defines an object type which can compactly represent an array of basic values: characters, integers, floating-point numbers. Arrays are mutable sequence types and behave very much like ..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20    <title>array — Efficient arrays of numeric values &#8212; Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21    22    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23    <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24    <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25    <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26    <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27    28    <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29    <script src="../_static/doctools.js?v=9bcbadda"></script>30    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31    <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32    33    <script src="../_static/sidebar.js"></script>34    35    <link rel="search" type="application/opensearchdescription+xml"36          title="Search within Python 3.15.0a6 documentation"37          href="../_static/opensearch.xml"/>38    <link rel="author" title="About these documents" href="../about.html" />39    <link rel="index" title="Index" href="../genindex.html" />40    <link rel="search" title="Search" href="../search.html" />41    <link rel="copyright" title="Copyright" href="../copyright.html" />42    <link rel="next" title="weakref — Weak references" href="weakref.html" />43    <link rel="prev" title="bisect — Array bisection algorithm" href="bisect.html" />44    45      46      <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47      48      <link rel="canonical" href="https://docs.python.org/3/library/array.html">49      50    51 52    53    <style>54      @media only screen {55        table.full-width-table {56            width: 100%;57        }58      }59    </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61    <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62            <script type="text/javascript" src="../_static/copybutton.js"></script>63            <script type="text/javascript" src="../_static/menu.js"></script>64            <script type="text/javascript" src="../_static/search-focus.js"></script>65            <script type="text/javascript" src="../_static/themetoggle.js"></script> 66            <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67            <meta name="readthedocs-addons-api-version" content="1">68 69  </head>70<body>71<div class="mobile-nav">72    <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73           aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74    <nav class="nav-content" role="navigation">75        <label for="menuToggler" class="toggler__label">76            <span></span>77        </label>78        <span class="nav-items-wrapper">79            <a href="https://www.python.org/" class="nav-logo">80                <img src="../_static/py.svg" alt="Python logo">81            </a>82            <span class="version_switcher_placeholder"></span>83            <form role="search" class="search" action="../search.html" method="get">84                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85                    <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86                </svg>87                <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88                <input type="submit" value="Go">89            </form>90        </span>91    </nav>92    <div class="menu-wrapper">93        <nav class="menu" role="navigation" aria-label="main navigation">94            <div class="language_switcher_placeholder"></div>95            96<label class="theme-selector-label">97    Theme98    <select class="theme-selector" oninput="activateTheme(this.value)">99        <option value="auto" selected>Auto</option>100        <option value="light">Light</option>101        <option value="dark">Dark</option>102    </select>103</label>104  <div>105    <h4>Previous topic</h4>106    <p class="topless"><a href="bisect.html"107                          title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bisect</span></code> — Array bisection algorithm</a></p>108  </div>109  <div>110    <h4>Next topic</h4>111    <p class="topless"><a href="weakref.html"112                          title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code> — Weak references</a></p>113  </div>114  <script>115    document.addEventListener('DOMContentLoaded', () => {116        const title = document.querySelector('meta[property="og:title"]').content;117        const elements = document.querySelectorAll('.improvepage');118        const pageurl = window.location.href.split('?')[0];119        elements.forEach(element => {120            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));121            url.searchParams.set('pagetitle', title);122            url.searchParams.set('pageurl', pageurl);123            url.searchParams.set('pagesource', "library/array.rst");124            element.href = url.toString();125        });126    });127  </script>128  <div role="note" aria-label="source link">129    <h3>This page</h3>130    <ul class="this-page-menu">131      <li><a href="../bugs.html">Report a bug</a></li>132      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>133      <li>134        <a href="https://github.com/python/cpython/blob/main/Doc/library/array.rst?plain=1"135            rel="nofollow">Show source136        </a>137      </li>138      139    </ul>140  </div>141        </nav>142    </div>143</div>144 145  146    <div class="related" role="navigation" aria-label="Related">147      <h3>Navigation</h3>148      <ul>149        <li class="right" style="margin-right: 10px">150          <a href="../genindex.html" title="General Index"151             accesskey="I">index</a></li>152        <li class="right" >153          <a href="../py-modindex.html" title="Python Module Index"154             >modules</a> |</li>155        <li class="right" >156          <a href="weakref.html" title="weakref — Weak references"157             accesskey="N">next</a> |</li>158        <li class="right" >159          <a href="bisect.html" title="bisect — Array bisection algorithm"160             accesskey="P">previous</a> |</li>161 162          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>163          <li><a href="https://www.python.org/">Python</a> &#187;</li>164          <li class="switchers">165            <div class="language_switcher_placeholder"></div>166            <div class="version_switcher_placeholder"></div>167          </li>168          <li>169              170          </li>171    <li id="cpython-language-and-version">172      <a href="../index.html">3.15.0a6 Documentation</a> &#187;173    </li>174 175          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>176          <li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">Data Types</a> &#187;</li>177        <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code> — Efficient arrays of numeric values</a></li>178                <li class="right">179                    180 181    <div class="inline-search" role="search">182        <form class="inline-search" action="../search.html" method="get">183          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">184          <input type="submit" value="Go">185        </form>186    </div>187                     |188                </li>189            <li class="right">190<label class="theme-selector-label">191    Theme192    <select class="theme-selector" oninput="activateTheme(this.value)">193        <option value="auto" selected>Auto</option>194        <option value="light">Light</option>195        <option value="dark">Dark</option>196    </select>197</label> |</li>198            199      </ul>200    </div>    201 202    <div class="document">203      <div class="documentwrapper">204        <div class="bodywrapper">205          <div class="body" role="main">206            207  <section id="module-array">208<span id="array-efficient-arrays-of-numeric-values"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code> — Efficient arrays of numeric values<a class="headerlink" href="#module-array" title="Link to this heading">¶</a></h1>209<hr class="docutils" id="index-0" />210<p>This module defines an object type which can compactly represent an array of211basic values: characters, integers, floating-point numbers.  Arrays are mutable <a class="reference internal" href="../glossary.html#term-sequence"><span class="xref std std-term">sequence</span></a>212types and behave very much like lists, except that the type of objects stored in213them is constrained.  The type is specified at object creation time by using a214<em class="dfn">type code</em>, which is a single character.  The following type codes are215defined:</p>216<table class="docutils align-default">217<thead>218<tr class="row-odd"><th class="head"><p>Type code</p></th>219<th class="head"><p>C Type</p></th>220<th class="head"><p>Python Type</p></th>221<th class="head"><p>Minimum size in bytes</p></th>222<th class="head"><p>Notes</p></th>223</tr>224</thead>225<tbody>226<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'b'</span></code></p></td>227<td><p>signed char</p></td>228<td><p>int</p></td>229<td><p>1</p></td>230<td></td>231</tr>232<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'B'</span></code></p></td>233<td><p>unsigned char</p></td>234<td><p>int</p></td>235<td><p>1</p></td>236<td></td>237</tr>238<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'u'</span></code></p></td>239<td><p>wchar_t</p></td>240<td><p>Unicode character</p></td>241<td><p>2</p></td>242<td><p>(1)</p></td>243</tr>244<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'w'</span></code></p></td>245<td><p>Py_UCS4</p></td>246<td><p>Unicode character</p></td>247<td><p>4</p></td>248<td><p>(2)</p></td>249</tr>250<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'h'</span></code></p></td>251<td><p>signed short</p></td>252<td><p>int</p></td>253<td><p>2</p></td>254<td></td>255</tr>256<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'H'</span></code></p></td>257<td><p>unsigned short</p></td>258<td><p>int</p></td>259<td><p>2</p></td>260<td></td>261</tr>262<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'i'</span></code></p></td>263<td><p>signed int</p></td>264<td><p>int</p></td>265<td><p>2</p></td>266<td></td>267</tr>268<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'I'</span></code></p></td>269<td><p>unsigned int</p></td>270<td><p>int</p></td>271<td><p>2</p></td>272<td></td>273</tr>274<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'l'</span></code></p></td>275<td><p>signed long</p></td>276<td><p>int</p></td>277<td><p>4</p></td>278<td></td>279</tr>280<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'L'</span></code></p></td>281<td><p>unsigned long</p></td>282<td><p>int</p></td>283<td><p>4</p></td>284<td></td>285</tr>286<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'q'</span></code></p></td>287<td><p>signed long long</p></td>288<td><p>int</p></td>289<td><p>8</p></td>290<td></td>291</tr>292<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'Q'</span></code></p></td>293<td><p>unsigned long long</p></td>294<td><p>int</p></td>295<td><p>8</p></td>296<td></td>297</tr>298<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'f'</span></code></p></td>299<td><p>float</p></td>300<td><p>float</p></td>301<td><p>4</p></td>302<td></td>303</tr>304<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'d'</span></code></p></td>305<td><p>double</p></td>306<td><p>float</p></td>307<td><p>8</p></td>308<td></td>309</tr>310</tbody>311</table>312<p>Notes:</p>313<ol class="arabic">314<li><p>It can be 16 bits or 32 bits depending on the platform.</p>315<div class="versionchanged">316<p><span class="versionmodified changed">Changed in version 3.9: </span><code class="docutils literal notranslate"><span class="pre">array('u')</span></code> now uses <code class="xref c c-type docutils literal notranslate"><span class="pre">wchar_t</span></code> as C type instead of deprecated317<code class="docutils literal notranslate"><span class="pre">Py_UNICODE</span></code>. This change doesn’t affect its behavior because318<code class="docutils literal notranslate"><span class="pre">Py_UNICODE</span></code> is alias of <code class="xref c c-type docutils literal notranslate"><span class="pre">wchar_t</span></code> since Python 3.3.</p>319</div>320<div class="deprecated-removed">321<p><span class="versionmodified deprecated">Deprecated since version 3.3, will be removed in version 3.16: </span>Please migrate to <code class="docutils literal notranslate"><span class="pre">'w'</span></code> typecode.</p>322</div>323</li>324<li><div class="versionadded">325<p><span class="versionmodified added">Added in version 3.13.</span></p>326</div>327</li>328</ol>329<p>The actual representation of values is determined by the machine architecture330(strictly speaking, by the C implementation).  The actual size can be accessed331through the <a class="reference internal" href="#array.array.itemsize" title="array.array.itemsize"><code class="xref py py-attr docutils literal notranslate"><span class="pre">array.itemsize</span></code></a> attribute.</p>332<p>The module defines the following item:</p>333<dl class="py data">334<dt class="sig sig-object py" id="array.typecodes">335<span class="sig-prename descclassname"><span class="pre">array.</span></span><span class="sig-name descname"><span class="pre">typecodes</span></span><a class="headerlink" href="#array.typecodes" title="Link to this definition">¶</a></dt>336<dd><p>A string with all available type codes.</p>337</dd></dl>338 339<p>The module defines the following type:</p>340<dl class="py class">341<dt class="sig sig-object py" id="array.array">342<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">array.</span></span><span class="sig-name descname"><span class="pre">array</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typecode</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">initializer</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array" title="Link to this definition">¶</a></dt>343<dd><p>A new array whose items are restricted by <em>typecode</em>, and initialized344from the optional <em>initializer</em> value, which must be a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>345or <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> object, a Unicode string, or iterable over elements346of the appropriate type.</p>347<p>If given a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> or <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> object, the initializer348is passed to the new array’s <a class="reference internal" href="#array.array.frombytes" title="array.array.frombytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">frombytes()</span></code></a> method;349if given a Unicode string, the initializer is passed to the350<a class="reference internal" href="#array.array.fromunicode" title="array.array.fromunicode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromunicode()</span></code></a> method;351otherwise, the initializer’s iterator is passed to the <a class="reference internal" href="#array.array.extend" title="array.array.extend"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extend()</span></code></a> method352to add initial items to the array.</p>353<p>Array objects support the ordinary <a class="reference internal" href="stdtypes.html#typesseq-mutable"><span class="std std-ref">mutable</span></a> <a class="reference internal" href="../glossary.html#term-sequence"><span class="xref std std-term">sequence</span></a> operations of indexing, slicing,354concatenation, and multiplication.  When using slice assignment, the assigned355value must be an array object with the same type code; in all other cases,356<a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised. Array objects also implement the buffer interface,357and may be used wherever <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> are supported.</p>358<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">array.__new__</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">typecode</span></code>, <code class="docutils literal notranslate"><span class="pre">initializer</span></code>.</p>359<dl class="py attribute">360<dt class="sig sig-object py" id="array.array.typecode">361<span class="sig-name descname"><span class="pre">typecode</span></span><a class="headerlink" href="#array.array.typecode" title="Link to this definition">¶</a></dt>362<dd><p>The typecode character used to create the array.</p>363</dd></dl>364 365<dl class="py attribute">366<dt class="sig sig-object py" id="array.array.itemsize">367<span class="sig-name descname"><span class="pre">itemsize</span></span><a class="headerlink" href="#array.array.itemsize" title="Link to this definition">¶</a></dt>368<dd><p>The length in bytes of one array item in the internal representation.</p>369</dd></dl>370 371<dl class="py method">372<dt class="sig sig-object py" id="array.array.append">373<span class="sig-name descname"><span class="pre">append</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.append" title="Link to this definition">¶</a></dt>374<dd><p>Append a new item with value <em>x</em> to the end of the array.</p>375</dd></dl>376 377<dl class="py method">378<dt class="sig sig-object py" id="array.array.buffer_info">379<span class="sig-name descname"><span class="pre">buffer_info</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.buffer_info" title="Link to this definition">¶</a></dt>380<dd><p>Return a tuple <code class="docutils literal notranslate"><span class="pre">(address,</span> <span class="pre">length)</span></code> giving the current memory address and the381length in elements of the buffer used to hold array’s contents.  The size of the382memory buffer in bytes can be computed as <code class="docutils literal notranslate"><span class="pre">array.buffer_info()[1]</span> <span class="pre">*</span>383<span class="pre">array.itemsize</span></code>.  This is occasionally useful when working with low-level (and384inherently unsafe) I/O interfaces that require memory addresses, such as certain385<code class="xref c c-func docutils literal notranslate"><span class="pre">ioctl()</span></code> operations.  The returned numbers are valid as long as the array386exists and no length-changing operations are applied to it.</p>387<div class="admonition note">388<p class="admonition-title">Note</p>389<p>When using array objects from code written in C or C++ (the only way to390effectively make use of this information), it makes more sense to use the buffer391interface supported by array objects.  This method is maintained for backward392compatibility and should be avoided in new code.  The buffer interface is393documented in <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">Buffer Protocol</span></a>.</p>394</div>395</dd></dl>396 397<dl class="py method">398<dt class="sig sig-object py" id="array.array.byteswap">399<span class="sig-name descname"><span class="pre">byteswap</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.byteswap" title="Link to this definition">¶</a></dt>400<dd><p>“Byteswap” all items of the array.  This is only supported for values which are4011, 2, 4, or 8 bytes in size; for other types of values, <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is402raised.  It is useful when reading data from a file written on a machine with a403different byte order.</p>404</dd></dl>405 406<dl class="py method">407<dt class="sig sig-object py" id="array.array.count">408<span class="sig-name descname"><span class="pre">count</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.count" title="Link to this definition">¶</a></dt>409<dd><p>Return the number of occurrences of <em>x</em> in the array.</p>410</dd></dl>411 412<dl class="py method">413<dt class="sig sig-object py" id="array.array.extend">414<span class="sig-name descname"><span class="pre">extend</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.extend" title="Link to this definition">¶</a></dt>415<dd><p>Append items from <em>iterable</em> to the end of the array.  If <em>iterable</em> is another416array, it must have <em>exactly</em> the same type code; if not, <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> will417be raised.  If <em>iterable</em> is not an array, it must be iterable and its elements418must be the right type to be appended to the array.</p>419</dd></dl>420 421<dl class="py method">422<dt class="sig sig-object py" id="array.array.frombytes">423<span class="sig-name descname"><span class="pre">frombytes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">buffer</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.frombytes" title="Link to this definition">¶</a></dt>424<dd><p>Appends items from the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>, interpreting425its content as an array of machine values (as if it had been read426from a file using the <a class="reference internal" href="#array.array.fromfile" title="array.array.fromfile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromfile()</span></code></a> method).</p>427<div class="versionadded">428<p><span class="versionmodified added">Added in version 3.2: </span><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromstring()</span></code> is renamed to <code class="xref py py-meth docutils literal notranslate"><span class="pre">frombytes()</span></code> for clarity.</p>429</div>430</dd></dl>431 432<dl class="py method">433<dt class="sig sig-object py" id="array.array.fromfile">434<span class="sig-name descname"><span class="pre">fromfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.fromfile" title="Link to this definition">¶</a></dt>435<dd><p>Read <em>n</em> items (as machine values) from the <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> <em>f</em> and append436them to the end of the array.  If less than <em>n</em> items are available,437<a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a> is raised, but the items that were available are still438inserted into the array.</p>439</dd></dl>440 441<dl class="py method">442<dt class="sig sig-object py" id="array.array.fromlist">443<span class="sig-name descname"><span class="pre">fromlist</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">list</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.fromlist" title="Link to this definition">¶</a></dt>444<dd><p>Append items from the list.  This is equivalent to <code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">list:</span>445<span class="pre">a.append(x)</span></code> except that if there is a type error, the array is unchanged.</p>446</dd></dl>447 448<dl class="py method">449<dt class="sig sig-object py" id="array.array.fromunicode">450<span class="sig-name descname"><span class="pre">fromunicode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.fromunicode" title="Link to this definition">¶</a></dt>451<dd><p>Extends this array with data from the given Unicode string.452The array must have type code <code class="docutils literal notranslate"><span class="pre">'u'</span></code> or <code class="docutils literal notranslate"><span class="pre">'w'</span></code>; otherwise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.453Use <code class="docutils literal notranslate"><span class="pre">array.frombytes(unicodestring.encode(enc))</span></code> to append Unicode data to an454array of some other type.</p>455</dd></dl>456 457<dl class="py method">458<dt class="sig sig-object py" id="array.array.index">459<span class="sig-name descname"><span class="pre">index</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">start</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">stop</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.index" title="Link to this definition">¶</a></dt>460<dd><p>Return the smallest <em>i</em> such that <em>i</em> is the index of the first occurrence of461<em>x</em> in the array.  The optional arguments <em>start</em> and <em>stop</em> can be462specified to search for <em>x</em> within a subsection of the array.  Raise463<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if <em>x</em> is not found.</p>464<div class="versionchanged">465<p><span class="versionmodified changed">Changed in version 3.10: </span>Added optional <em>start</em> and <em>stop</em> parameters.</p>466</div>467</dd></dl>468 469<dl class="py method">470<dt class="sig sig-object py" id="array.array.insert">471<span class="sig-name descname"><span class="pre">insert</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">i</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.insert" title="Link to this definition">¶</a></dt>472<dd><p>Insert a new item with value <em>x</em> in the array before position <em>i</em>. Negative473values are treated as being relative to the end of the array.</p>474</dd></dl>475 476<dl class="py method">477<dt class="sig sig-object py" id="array.array.pop">478<span class="sig-name descname"><span class="pre">pop</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">i</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.pop" title="Link to this definition">¶</a></dt>479<dd><p>Removes the item with the index <em>i</em> from the array and returns it. The optional480argument defaults to <code class="docutils literal notranslate"><span class="pre">-1</span></code>, so that by default the last item is removed and481returned.</p>482</dd></dl>483 484<dl class="py method">485<dt class="sig sig-object py" id="array.array.remove">486<span class="sig-name descname"><span class="pre">remove</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.remove" title="Link to this definition">¶</a></dt>487<dd><p>Remove the first occurrence of <em>x</em> from the array.</p>488</dd></dl>489 490<dl class="py method">491<dt class="sig sig-object py" id="array.array.clear">492<span class="sig-name descname"><span class="pre">clear</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.clear" title="Link to this definition">¶</a></dt>493<dd><p>Remove all elements from the array.</p>494<div class="versionadded">495<p><span class="versionmodified added">Added in version 3.13.</span></p>496</div>497</dd></dl>498 499<dl class="py method">500<dt class="sig sig-object py" id="array.array.reverse">501<span class="sig-name descname"><span class="pre">reverse</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.reverse" title="Link to this definition">¶</a></dt>502<dd><p>Reverse the order of the items in the array.</p>503</dd></dl>504 505<dl class="py method">506<dt class="sig sig-object py" id="array.array.tobytes">507<span class="sig-name descname"><span class="pre">tobytes</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tobytes" title="Link to this definition">¶</a></dt>508<dd><p>Convert the array to an array of machine values and return the bytes509representation (the same sequence of bytes that would be written to a file by510the <a class="reference internal" href="#array.array.tofile" title="array.array.tofile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tofile()</span></code></a> method.)</p>511<div class="versionadded">512<p><span class="versionmodified added">Added in version 3.2: </span><code class="xref py py-meth docutils literal notranslate"><span class="pre">tostring()</span></code> is renamed to <code class="xref py py-meth docutils literal notranslate"><span class="pre">tobytes()</span></code> for clarity.</p>513</div>514</dd></dl>515 516<dl class="py method">517<dt class="sig sig-object py" id="array.array.tofile">518<span class="sig-name descname"><span class="pre">tofile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">f</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tofile" title="Link to this definition">¶</a></dt>519<dd><p>Write all items (as machine values) to the <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> <em>f</em>.</p>520</dd></dl>521 522<dl class="py method">523<dt class="sig sig-object py" id="array.array.tolist">524<span class="sig-name descname"><span class="pre">tolist</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tolist" title="Link to this definition">¶</a></dt>525<dd><p>Convert the array to an ordinary list with the same items.</p>526</dd></dl>527 528<dl class="py method">529<dt class="sig sig-object py" id="array.array.tounicode">530<span class="sig-name descname"><span class="pre">tounicode</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tounicode" title="Link to this definition">¶</a></dt>531<dd><p>Convert the array to a Unicode string.  The array must have a type <code class="docutils literal notranslate"><span class="pre">'u'</span></code> or <code class="docutils literal notranslate"><span class="pre">'w'</span></code>;532otherwise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised. Use <code class="docutils literal notranslate"><span class="pre">array.tobytes().decode(enc)</span></code> to533obtain a Unicode string from an array of some other type.</p>534</dd></dl>535 536</dd></dl>537 538<p>The string representation of array objects has the form539<code class="docutils literal notranslate"><span class="pre">array(typecode,</span> <span class="pre">initializer)</span></code>.540The <em>initializer</em> is omitted if the array is empty, otherwise it is541a Unicode string if the <em>typecode</em> is <code class="docutils literal notranslate"><span class="pre">'u'</span></code> or <code class="docutils literal notranslate"><span class="pre">'w'</span></code>, otherwise it is542a list of numbers.543The string representation is guaranteed to be able to be converted back to an544array with the same type and value using <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>, so long as the545<a class="reference internal" href="#array.array" title="array.array"><code class="xref py py-class docutils literal notranslate"><span class="pre">array</span></code></a> class has been imported using <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">array</span> <span class="pre">import</span> <span class="pre">array</span></code>.546Variables <code class="docutils literal notranslate"><span class="pre">inf</span></code> and <code class="docutils literal notranslate"><span class="pre">nan</span></code> must also be defined if it contains547corresponding floating-point values.548Examples:</p>549<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">array</span><span class="p">(</span><span class="s1">&#39;l&#39;</span><span class="p">)</span>550<span class="n">array</span><span class="p">(</span><span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="s1">&#39;hello </span><span class="se">\u2641</span><span class="s1">&#39;</span><span class="p">)</span>551<span class="n">array</span><span class="p">(</span><span class="s1">&#39;l&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span>552<span class="n">array</span><span class="p">(</span><span class="s1">&#39;d&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.14</span><span class="p">,</span> <span class="o">-</span><span class="n">inf</span><span class="p">,</span> <span class="n">nan</span><span class="p">])</span>553</pre></div>554</div>555<div class="admonition seealso">556<p class="admonition-title">See also</p>557<dl class="simple">558<dt>Module <a class="reference internal" href="struct.html#module-struct" title="struct: Interpret bytes as packed binary data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code></a></dt><dd><p>Packing and unpacking of heterogeneous binary data.</p>559</dd>560<dt><a class="reference external" href="https://numpy.org/">NumPy</a></dt><dd><p>The NumPy package defines another array type.</p>561</dd>562</dl>563</div>564</section>565 566 567            <div class="clearer"></div>568          </div>569        </div>570      </div>571      <div class="sphinxsidebar" role="navigation" aria-label="Main">572        <div class="sphinxsidebarwrapper">573  <div>574    <h4>Previous topic</h4>575    <p class="topless"><a href="bisect.html"576                          title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bisect</span></code> — Array bisection algorithm</a></p>577  </div>578  <div>579    <h4>Next topic</h4>580    <p class="topless"><a href="weakref.html"581                          title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code> — Weak references</a></p>582  </div>583  <script>584    document.addEventListener('DOMContentLoaded', () => {585        const title = document.querySelector('meta[property="og:title"]').content;586        const elements = document.querySelectorAll('.improvepage');587        const pageurl = window.location.href.split('?')[0];588        elements.forEach(element => {589            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));590            url.searchParams.set('pagetitle', title);591            url.searchParams.set('pageurl', pageurl);592            url.searchParams.set('pagesource', "library/array.rst");593            element.href = url.toString();594        });595    });596  </script>597  <div role="note" aria-label="source link">598    <h3>This page</h3>599    <ul class="this-page-menu">600      <li><a href="../bugs.html">Report a bug</a></li>601      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>602      <li>603        <a href="https://github.com/python/cpython/blob/main/Doc/library/array.rst?plain=1"604            rel="nofollow">Show source605        </a>606      </li>607      608    </ul>609  </div>610        </div>611<div id="sidebarbutton" title="Collapse sidebar">612<span>«</span>613</div>614 615      </div>616      <div class="clearer"></div>617    </div>  618    <div class="related" role="navigation" aria-label="Related">619      <h3>Navigation</h3>620      <ul>621        <li class="right" style="margin-right: 10px">622          <a href="../genindex.html" title="General Index"623             >index</a></li>624        <li class="right" >625          <a href="../py-modindex.html" title="Python Module Index"626             >modules</a> |</li>627        <li class="right" >628          <a href="weakref.html" title="weakref — Weak references"629             >next</a> |</li>630        <li class="right" >631          <a href="bisect.html" title="bisect — Array bisection algorithm"632             >previous</a> |</li>633 634          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>635          <li><a href="https://www.python.org/">Python</a> &#187;</li>636          <li class="switchers">637            <div class="language_switcher_placeholder"></div>638            <div class="version_switcher_placeholder"></div>639          </li>640          <li>641              642          </li>643    <li id="cpython-language-and-version">644      <a href="../index.html">3.15.0a6 Documentation</a> &#187;645    </li>646 647          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>648          <li class="nav-item nav-item-2"><a href="datatypes.html" >Data Types</a> &#187;</li>649        <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code> — Efficient arrays of numeric values</a></li>650                <li class="right">651                    652 653    <div class="inline-search" role="search">654        <form class="inline-search" action="../search.html" method="get">655          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">656          <input type="submit" value="Go">657        </form>658    </div>659                     |660                </li>661            <li class="right">662<label class="theme-selector-label">663    Theme664    <select class="theme-selector" oninput="activateTheme(this.value)">665        <option value="auto" selected>Auto</option>666        <option value="light">Light</option>667        <option value="dark">Dark</option>668    </select>669</label> |</li>670            671      </ul>672    </div>  673    <div class="footer">674    &copy; <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.675    <br>676    This page is licensed under the Python Software Foundation License Version 2.677    <br>678    Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.679    <br>680    681      See <a href="/license.html">History and License</a> for more information.<br>682    683    684    <br>685 686    The Python Software Foundation is a non-profit corporation.687<a href="https://www.python.org/psf/donations/">Please donate.</a>688<br>689    <br>690      Last updated on Mar 10, 2026 (08:58 UTC).691    692      <a href="/bugs.html">Found a bug</a>?693    694    <br>695 696    Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.697    </div>698 699  </body>700</html>