Files
FootPrint/Python/pythoncore-3.15-64/Doc/html/builtins/time-complexity.html
T

815 lines
59 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta property="og:title" content="Time complexity of operations on built-in types" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://docs.python.org/3/builtins/time-complexity.html" />
<meta property="og:site_name" content="Python documentation" />
<meta property="og:description" content="This page documents the time complexity of various operations on built-in types in CPython. Other Python implementations may have different performance characteristics. Additionally, the listed cos..." />
<meta property="og:image" content="_static/og-image.png" />
<meta property="og:image:alt" content="Python documentation" />
<meta name="description" content="This page documents the time complexity of various operations on built-in types in CPython. Other Python implementations may have different performance characteristics. Additionally, the listed cos..." />
<meta name="theme-color" content="#3776ab">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<title>Time complexity of operations on built-in types &#8212; Python 3.15.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=48df1187" />
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=0fc419ee" />
<script src="../_static/documentation_options.js?v=0fe728b1"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python 3.15.0 documentation"
href="../_static/opensearch.xml"/>
<link rel="author" title="About these documents" href="../about.html" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="next" title="The Python standard library" href="../library/index.html" />
<link rel="prev" title="Thread Safety Guarantees" href="threadsafety.html" />
<link rel="canonical" href="https://docs.python.org/3/builtins/time-complexity.html">
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
<link rel="shortcut icon" type="image/png" href="../_static/py.svg">
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
<script type="text/javascript" src="../_static/search-focus.js"></script>
<script type="text/javascript" src="../_static/themetoggle.js"></script>
<script type="text/javascript" src="../_static/rtd_switcher.js"></script>
<meta name="readthedocs-addons-api-version" content="1">
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">
<nav class="nav-content" role="navigation">
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<span class="nav-items-wrapper">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Python logo">
</a>
<span class="version_switcher_placeholder"></span>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<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>
</svg>
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q">
<input type="submit" value="Go">
</form>
</span>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<div>
<h3><a href="../contents.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Time complexity of operations on built-in types</a><ul>
<li><a class="reference internal" href="#list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a></li>
<li><a class="reference internal" href="#tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a></li>
<li><a class="reference internal" href="#dict-frozendict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">frozendict</span></code></a></li>
<li><a class="reference internal" href="#set-frozenset"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">frozenset</span></code></a></li>
<li><a class="reference internal" href="#str-bytes-bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a></li>
<li><a class="reference internal" href="#memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a></li>
<li><a class="reference internal" href="#range"><code class="xref py py-class docutils literal notranslate"><span class="pre">range</span></code></a></li>
<li><a class="reference internal" href="#notes">Notes</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Previous topic</h4>
<p class="topless"><a href="threadsafety.html"
title="previous chapter">Thread Safety Guarantees</a></p>
</div>
<div>
<h4>Next topic</h4>
<p class="topless"><a href="../library/index.html"
title="next chapter">The Python standard library</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "builtins/time-complexity.rst");
element.href = url.toString();
});
});
</script>
<div role="note" aria-label="source link">
<h3>This page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a bug</a></li>
<li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.15/Doc/builtins/time-complexity.rst?plain=1"
rel="nofollow">Show source
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="../library/index.html" title="The Python standard library"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="threadsafety.html" title="Thread Safety Guarantees"
accesskey="P">previous</a> |</li>
<li class="root-icon"><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.15.0 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python built-ins reference</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">Time complexity of operations on built-in types</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">
<input type="submit" value="Go">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="time-complexity-of-operations-on-built-in-types">
<span id="time-complexity"></span><h1>Time complexity of operations on built-in types<a class="headerlink" href="#time-complexity-of-operations-on-built-in-types" title="Link to this heading"></a></h1>
<p>This page documents the time complexity of various operations on built-in types
in CPython. Other Python implementations may have different performance
characteristics. Additionally, the listed costs assume exact built-in types, as
instances of subclasses may have different costs.</p>
<p>We use <a class="reference external" href="https://en.wikipedia.org/wiki/Big_O_notation">Big <em>O</em> notation</a> to describe how the running time of an operation grows
with the size of its inputs. Unless stated otherwise, <em>n</em> denotes the number of
elements currently in the container, and <em>k</em> is the value of a numeric
parameter, such as an index or a repeat count.</p>
<section id="list">
<h2><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code><a class="headerlink" href="#list" title="Link to this heading"></a></h2>
<p>Lists are mutable sequences; for more detail on the implementation see
<a class="reference internal" href="../faq/design.html#how-are-lists-implemented"><span class="std std-ref">How are lists implemented in CPython?</span></a>. The largest costs come from growing beyond the
current allocation size (because everything must move), or from inserting or
deleting somewhere near the beginning (because everything after that must move).
If you need to add or remove at both ends, consider using a
<a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.deque</span></code></a> instead.</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Operation</p></th>
<th class="head"><p>Complexity</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>Copy (<code class="docutils literal notranslate"><span class="pre">l.copy()</span></code>)</p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-odd"><td><p>Append (<code class="docutils literal notranslate"><span class="pre">l.append(x)</span></code>) <a class="footnote-reference brackets" href="#id54" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-even"><td><p>Pop (<code class="docutils literal notranslate"><span class="pre">l.pop(k)</span></code>) <a class="footnote-reference brackets" href="#id54" id="id2" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id55" id="id3" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(<em>n</em> - <em>k</em>)</p></td>
</tr>
<tr class="row-odd"><td><p>Insert (<code class="docutils literal notranslate"><span class="pre">l.insert(k,</span> <span class="pre">x)</span></code>) <a class="footnote-reference brackets" href="#id54" id="id4" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id55" id="id5" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(<em>n</em> - <em>k</em>)</p></td>
</tr>
<tr class="row-even"><td><p>Get item (<code class="docutils literal notranslate"><span class="pre">l[k]</span></code>)</p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-odd"><td><p>Set item (<code class="docutils literal notranslate"><span class="pre">l[k]</span> <span class="pre">=</span> <span class="pre">x</span></code>)</p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-even"><td><p>Delete item (<code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">l[k]</span></code>) <a class="footnote-reference brackets" href="#id55" id="id6" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(<em>n</em> - <em>k</em>)</p></td>
</tr>
<tr class="row-odd"><td><p>Iteration</p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-even"><td><p>Get slice (<code class="docutils literal notranslate"><span class="pre">l[i:j]</span></code>)</p></td>
<td><p><em>O</em>(<em>j</em> - <em>i</em>)</p></td>
</tr>
<tr class="row-odd"><td><p>Set slice (<code class="docutils literal notranslate"><span class="pre">l[i:j]</span> <span class="pre">=</span> <span class="pre">t</span></code>) <a class="footnote-reference brackets" href="#id54" id="id7" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(<em>j</em> - <em>i</em>) if len(<em>t</em>) == <em>j</em> - <em>i</em>,
otherwise <em>O</em>(<em>n</em> - <em>i</em> + len(<em>t</em>))</p></td>
</tr>
<tr class="row-even"><td><p>Delete slice (<code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">l[i:j]</span></code>)</p></td>
<td><p><em>O</em>(<em>n</em> - <em>i</em>)</p></td>
</tr>
<tr class="row-odd"><td><p>Extend (<code class="docutils literal notranslate"><span class="pre">l.extend(t)</span></code>) <a class="footnote-reference brackets" href="#id54" id="id8" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id56" id="id9" role="doc-noteref"><span class="fn-bracket">[</span>3<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(len(<em>t</em>))</p></td>
</tr>
<tr class="row-even"><td><p>Sort (<code class="docutils literal notranslate"><span class="pre">l.sort()</span></code>) <a class="footnote-reference brackets" href="#id57" id="id10" role="doc-noteref"><span class="fn-bracket">[</span>4<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(<em>n</em> log <em>n</em>)</p></td>
</tr>
<tr class="row-odd"><td><p>Concatenate (<code class="docutils literal notranslate"><span class="pre">l1</span> <span class="pre">+</span> <span class="pre">l2</span></code>)</p></td>
<td><p><em>O</em>(len(<em>l1</em>) + len(<em>l2</em>))</p></td>
</tr>
<tr class="row-even"><td><p>Multiply (<code class="docutils literal notranslate"><span class="pre">l</span> <span class="pre">*</span> <span class="pre">k</span></code>)</p></td>
<td><p><em>O</em>(<em>nk</em>)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">l</span></code></p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">min(l)</span></code>, <code class="docutils literal notranslate"><span class="pre">max(l)</span></code></p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-odd"><td><p>Get length (<code class="docutils literal notranslate"><span class="pre">len(l)</span></code>) <a class="footnote-reference brackets" href="#id58" id="id11" role="doc-noteref"><span class="fn-bracket">[</span>5<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
</tbody>
</table>
</section>
<section id="tuple">
<h2><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code><a class="headerlink" href="#tuple" title="Link to this heading"></a></h2>
<p>A <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a> is an <a class="reference internal" href="../glossary.html#term-immutable"><span class="xref std std-term">immutable</span></a> sequence. Because a tuple can never
change, there are no insertion or deletion costs, and making a copy simply
returns the same object, so is constant time (<em>O</em>(1)).</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Operation</p></th>
<th class="head"><p>Complexity</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>Copy (<code class="docutils literal notranslate"><span class="pre">tuple(t)</span></code>)</p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-odd"><td><p>Get item (<code class="docutils literal notranslate"><span class="pre">t[k]</span></code>)</p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-even"><td><p>Get slice (<code class="docutils literal notranslate"><span class="pre">t[i:j]</span></code>)</p></td>
<td><p><em>O</em>(<em>j</em> - <em>i</em>)</p></td>
</tr>
<tr class="row-odd"><td><p>Concatenate (<code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">+</span> <span class="pre">t2</span></code>)</p></td>
<td><p><em>O</em>(len(<em>t1</em>) + len(<em>t2</em>))</p></td>
</tr>
<tr class="row-even"><td><p>Multiply (<code class="docutils literal notranslate"><span class="pre">t</span> <span class="pre">*</span> <span class="pre">k</span></code>)</p></td>
<td><p><em>O</em>(<em>nk</em>)</p></td>
</tr>
<tr class="row-odd"><td><p>Iteration</p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">t</span></code></p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">min(t)</span></code>, <code class="docutils literal notranslate"><span class="pre">max(t)</span></code></p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-even"><td><p>Get length (<code class="docutils literal notranslate"><span class="pre">len(t)</span></code>) <a class="footnote-reference brackets" href="#id58" id="id12" role="doc-noteref"><span class="fn-bracket">[</span>5<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
</tbody>
</table>
</section>
<section id="dict-frozendict">
<h2><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">frozendict</span></code><a class="headerlink" href="#dict-frozendict" title="Link to this heading"></a></h2>
<p>The times listed for dict objects are average-case times, as they assume the
hash function for the objects is sufficiently robust to make collisions
uncommon. They also assume the keys are well-distributed among the set of
possible keys. In the worst case, when every key hashes to the same value,
each of the <em>O</em>(1) operations below instead takes <em>O</em>(<em>n</em>) time. They also
assume that hashing and comparing a key is <em>O</em>(1). For more detail on the
implementation, see <a class="reference internal" href="../faq/design.html#how-are-dictionaries-implemented"><span class="std std-ref">How are dictionaries implemented in CPython?</span></a>.</p>
<p>A <a class="reference internal" href="stdtypes.html#frozendict" title="frozendict"><code class="xref py py-class docutils literal notranslate"><span class="pre">frozendict</span></code></a> is immutable, so it does not support setting, deleting,
or updating items. The other operations below apply to it at the same costs.</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Operation</p></th>
<th class="head"><p>Complexity</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">key</span> <span class="pre">in</span> <span class="pre">d</span></code></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-odd"><td><p>Copy (<code class="docutils literal notranslate"><span class="pre">d.copy()</span></code>) <a class="footnote-reference brackets" href="#id59" id="id13" role="doc-noteref"><span class="fn-bracket">[</span>6<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id60" id="id14" role="doc-noteref"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-even"><td><p>Get item (<code class="docutils literal notranslate"><span class="pre">d[key]</span></code>, <code class="docutils literal notranslate"><span class="pre">d.get(key)</span></code>)</p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-odd"><td><p>Set item (<code class="docutils literal notranslate"><span class="pre">d[key]</span> <span class="pre">=</span> <span class="pre">value</span></code>) <a class="footnote-reference brackets" href="#id54" id="id15" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-even"><td><p>Delete item (<code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">d[key]</span></code>, <code class="docutils literal notranslate"><span class="pre">d.pop(key)</span></code>)</p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-odd"><td><p>Update (<code class="docutils literal notranslate"><span class="pre">d.update(t)</span></code>, <code class="docutils literal notranslate"><span class="pre">d</span> <span class="pre">|=</span> <span class="pre">t</span></code>) <a class="footnote-reference brackets" href="#id54" id="id16" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id56" id="id17" role="doc-noteref"><span class="fn-bracket">[</span>3<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id60" id="id18" role="doc-noteref"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(len(<em>t</em>))</p></td>
</tr>
<tr class="row-even"><td><p>Iteration <a class="footnote-reference brackets" href="#id60" id="id19" role="doc-noteref"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-odd"><td><p>Get length (<code class="docutils literal notranslate"><span class="pre">len(d)</span></code>) <a class="footnote-reference brackets" href="#id58" id="id20" role="doc-noteref"><span class="fn-bracket">[</span>5<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
</tbody>
</table>
</section>
<section id="set-frozenset">
<h2><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">frozenset</span></code><a class="headerlink" href="#set-frozenset" title="Link to this heading"></a></h2>
<p>See <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> as the <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> and <a class="reference internal" href="stdtypes.html#frozenset" title="frozenset"><code class="xref py py-class docutils literal notranslate"><span class="pre">frozenset</span></code></a> implementations are
similar, and the same caveats apply.
In the worst case, <em>O</em>(1) operations instead take <em>O</em>(<em>n</em>) time,
and operations that look up every element degrade accordingly.</p>
<p>A <a class="reference internal" href="stdtypes.html#frozenset" title="frozenset"><code class="xref py py-class docutils literal notranslate"><span class="pre">frozenset</span></code></a> is <a class="reference internal" href="../glossary.html#term-immutable"><span class="xref std std-term">immutable</span></a>, so it does not support adding,
discarding, or the in-place update operations. The others below apply to it at
the same costs.</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Operation</p></th>
<th class="head"><p>Complexity</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">s</span></code></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-odd"><td><p>Copy (<code class="docutils literal notranslate"><span class="pre">s.copy()</span></code>) <a class="footnote-reference brackets" href="#id59" id="id21" role="doc-noteref"><span class="fn-bracket">[</span>6<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id60" id="id22" role="doc-noteref"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-even"><td><p>Add (<code class="docutils literal notranslate"><span class="pre">s.add(x)</span></code>) <a class="footnote-reference brackets" href="#id54" id="id23" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-odd"><td><p>Discard (<code class="docutils literal notranslate"><span class="pre">s.discard(x)</span></code>, <code class="docutils literal notranslate"><span class="pre">s.remove(x)</span></code>)</p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-even"><td><p>Union (<code class="docutils literal notranslate"><span class="pre">s1</span> <span class="pre">|</span> <span class="pre">s2</span></code>, <code class="docutils literal notranslate"><span class="pre">s1.union(s2)</span></code>) <a class="footnote-reference brackets" href="#id60" id="id24" role="doc-noteref"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(len(<em>s1</em>) + len(<em>s2</em>))</p></td>
</tr>
<tr class="row-odd"><td><p>Update (<code class="docutils literal notranslate"><span class="pre">s1</span> <span class="pre">|=</span> <span class="pre">s2</span></code>, <code class="docutils literal notranslate"><span class="pre">s1.update(s2)</span></code>) <a class="footnote-reference brackets" href="#id54" id="id25" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id60" id="id26" role="doc-noteref"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(len(<em>s2</em>))</p></td>
</tr>
<tr class="row-even"><td><p>Intersection (<code class="docutils literal notranslate"><span class="pre">s1</span> <span class="pre">&amp;</span> <span class="pre">s2</span></code>, <code class="docutils literal notranslate"><span class="pre">s1.intersection(s2)</span></code>) <a class="footnote-reference brackets" href="#id60" id="id27" role="doc-noteref"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id61" id="id28" role="doc-noteref"><span class="fn-bracket">[</span>8<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(min(len(<em>s1</em>), len(<em>s2</em>)))</p></td>
</tr>
<tr class="row-odd"><td><p>Intersection update (<code class="docutils literal notranslate"><span class="pre">s1</span> <span class="pre">&amp;=</span> <span class="pre">s2</span></code>, <code class="docutils literal notranslate"><span class="pre">s1.intersection_update(s2)</span></code>) <a class="footnote-reference brackets" href="#id54" id="id29" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id60" id="id30" role="doc-noteref"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id61" id="id31" role="doc-noteref"><span class="fn-bracket">[</span>8<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(min(len(<em>s1</em>), len(<em>s2</em>)))</p></td>
</tr>
<tr class="row-even"><td><p>Difference (<code class="docutils literal notranslate"><span class="pre">s1</span> <span class="pre">-</span> <span class="pre">s2</span></code>, <code class="docutils literal notranslate"><span class="pre">s1.difference(s2)</span></code>) <a class="footnote-reference brackets" href="#id60" id="id32" role="doc-noteref"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id62" id="id33" role="doc-noteref"><span class="fn-bracket">[</span>9<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(len(<em>s1</em>))</p></td>
</tr>
<tr class="row-odd"><td><p>Difference update (<code class="docutils literal notranslate"><span class="pre">s1</span> <span class="pre">-=</span> <span class="pre">s2</span></code>, <code class="docutils literal notranslate"><span class="pre">s1.difference_update(s2)</span></code>) <a class="footnote-reference brackets" href="#id54" id="id34" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id60" id="id35" role="doc-noteref"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id61" id="id36" role="doc-noteref"><span class="fn-bracket">[</span>8<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(min(len(<em>s1</em>), len(<em>s2</em>)))</p></td>
</tr>
<tr class="row-even"><td><p>Symmetric difference (<code class="docutils literal notranslate"><span class="pre">s1</span> <span class="pre">^</span> <span class="pre">s2</span></code>, <code class="docutils literal notranslate"><span class="pre">s1.symmetric_difference(s2)</span></code>) <a class="footnote-reference brackets" href="#id60" id="id37" role="doc-noteref"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(len(<em>s1</em>) + len(<em>s2</em>))</p></td>
</tr>
<tr class="row-odd"><td><p>Symmetric difference update (<code class="docutils literal notranslate"><span class="pre">s1</span> <span class="pre">^=</span> <span class="pre">s2</span></code>, <code class="docutils literal notranslate"><span class="pre">s1.symmetric_difference_update(s2)</span></code>) <a class="footnote-reference brackets" href="#id54" id="id38" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id60" id="id39" role="doc-noteref"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(len(<em>s2</em>))</p></td>
</tr>
<tr class="row-even"><td><p>Get length (<code class="docutils literal notranslate"><span class="pre">len(s)</span></code>) <a class="footnote-reference brackets" href="#id58" id="id40" role="doc-noteref"><span class="fn-bracket">[</span>5<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
</tbody>
</table>
</section>
<section id="str-bytes-bytearray">
<h2><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code><a class="headerlink" href="#str-bytes-bytearray" title="Link to this heading"></a></h2>
<p><a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> and <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> objects are immutable sequences of characters and
bytes, respectively. As with tuples, copying one returns the original object.
A <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> is mutable, and additionally supports the mutating operations
of <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> (except <code class="xref py py-meth docutils literal notranslate"><span class="pre">sort()</span></code>), at the same costs. However, deleting at
the front with <code class="docutils literal notranslate"><span class="pre">del</span></code> (<code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">b[0]</span></code>, <code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">b[:k]</span></code>) only advances the start of
the buffer instead of moving the remaining bytes, and is amortized <em>O</em>(1).</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Operation</p></th>
<th class="head"><p>Complexity</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>Get item (<code class="docutils literal notranslate"><span class="pre">s[k]</span></code>)</p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-odd"><td><p>Get slice (<code class="docutils literal notranslate"><span class="pre">s[i:j]</span></code>)</p></td>
<td><p><em>O</em>(<em>j</em> - <em>i</em>)</p></td>
</tr>
<tr class="row-even"><td><p>Concatenate (<code class="docutils literal notranslate"><span class="pre">s</span> <span class="pre">+</span> <span class="pre">t</span></code>) <a class="footnote-reference brackets" href="#id63" id="id41" role="doc-noteref"><span class="fn-bracket">[</span>10<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(len(<em>s</em>) + len(<em>t</em>))</p></td>
</tr>
<tr class="row-odd"><td><p>Multiply (<code class="docutils literal notranslate"><span class="pre">s</span> <span class="pre">*</span> <span class="pre">k</span></code>)</p></td>
<td><p><em>O</em>(<em>nk</em>)</p></td>
</tr>
<tr class="row-even"><td><p>Substring search (<code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">s</span></code>, <code class="docutils literal notranslate"><span class="pre">s.find(x)</span></code>, <code class="docutils literal notranslate"><span class="pre">s.index(x)</span></code>) <a class="footnote-reference brackets" href="#id64" id="id42" role="doc-noteref"><span class="fn-bracket">[</span>11<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-odd"><td><p>Reverse substring search (<code class="docutils literal notranslate"><span class="pre">s.rfind(x)</span></code>, <code class="docutils literal notranslate"><span class="pre">s.rindex(x)</span></code>) <a class="footnote-reference brackets" href="#id64" id="id43" role="doc-noteref"><span class="fn-bracket">[</span>11<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id65" id="id44" role="doc-noteref"><span class="fn-bracket">[</span>12<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(<em>n</em> × len(<em>x</em>))</p></td>
</tr>
<tr class="row-even"><td><p>Encode or decode <a class="footnote-reference brackets" href="#id66" id="id45" role="doc-noteref"><span class="fn-bracket">[</span>13<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-odd"><td><p>Iteration</p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-even"><td><p>Get length (<code class="docutils literal notranslate"><span class="pre">len(s)</span></code>) <a class="footnote-reference brackets" href="#id58" id="id46" role="doc-noteref"><span class="fn-bracket">[</span>5<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
</tbody>
</table>
</section>
<section id="memoryview">
<h2><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code><a class="headerlink" href="#memoryview" title="Link to this heading"></a></h2>
<p><a class="reference internal" href="stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a> objects allow Python code to access the internal data
of an object that supports the <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">buffer protocol</span></a> without
copying. In particular, slicing a memory view returns a new view onto the same
buffer.</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Operation</p></th>
<th class="head"><p>Complexity</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>Create (<code class="docutils literal notranslate"><span class="pre">memoryview(obj)</span></code>)</p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-odd"><td><p>Get item (<code class="docutils literal notranslate"><span class="pre">v[k]</span></code>)</p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-even"><td><p>Get slice (<code class="docutils literal notranslate"><span class="pre">v[i:j]</span></code>)</p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-odd"><td><p>Index (<code class="docutils literal notranslate"><span class="pre">v.index(x)</span></code>) <a class="footnote-reference brackets" href="#id64" id="id47" role="doc-noteref"><span class="fn-bracket">[</span>11<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id67" id="id48" role="doc-noteref"><span class="fn-bracket">[</span>14<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-even"><td><p>Count (<code class="docutils literal notranslate"><span class="pre">v.count(x)</span></code>) <a class="footnote-reference brackets" href="#id67" id="id49" role="doc-noteref"><span class="fn-bracket">[</span>14<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-odd"><td><p>Convert to bytes (<code class="docutils literal notranslate"><span class="pre">v.tobytes()</span></code>, <code class="docutils literal notranslate"><span class="pre">bytes(v)</span></code>)</p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-even"><td><p>Get length (<code class="docutils literal notranslate"><span class="pre">len(v)</span></code>) <a class="footnote-reference brackets" href="#id58" id="id50" role="doc-noteref"><span class="fn-bracket">[</span>5<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
</tbody>
</table>
</section>
<section id="range">
<h2><code class="xref py py-class docutils literal notranslate"><span class="pre">range</span></code><a class="headerlink" href="#range" title="Link to this heading"></a></h2>
<p>A <a class="reference internal" href="stdtypes.html#range" title="range"><code class="xref py py-class docutils literal notranslate"><span class="pre">range</span></code></a> object computes its items on demand from its <em>start</em>, <em>stop</em> and
<em>step</em> values, so most operations do not depend on the length of the range.</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Operation</p></th>
<th class="head"><p>Complexity</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>Get item (<code class="docutils literal notranslate"><span class="pre">r[k]</span></code>)</p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-odd"><td><p>Get slice (<code class="docutils literal notranslate"><span class="pre">r[i:j]</span></code>)</p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">r</span></code> <a class="footnote-reference brackets" href="#id68" id="id51" role="doc-noteref"><span class="fn-bracket">[</span>15<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-odd"><td><p>Index and count (<code class="docutils literal notranslate"><span class="pre">r.index(x)</span></code>, <code class="docutils literal notranslate"><span class="pre">r.count(x)</span></code>) <a class="footnote-reference brackets" href="#id68" id="id52" role="doc-noteref"><span class="fn-bracket">[</span>15<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
<tr class="row-even"><td><p>Iteration</p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">min(r)</span></code>, <code class="docutils literal notranslate"><span class="pre">max(r)</span></code></p></td>
<td><p><em>O</em>(<em>n</em>)</p></td>
</tr>
<tr class="row-even"><td><p>Get length (<code class="docutils literal notranslate"><span class="pre">len(r)</span></code>) <a class="footnote-reference brackets" href="#id58" id="id53" role="doc-noteref"><span class="fn-bracket">[</span>5<span class="fn-bracket">]</span></a></p></td>
<td><p><em>O</em>(1)</p></td>
</tr>
</tbody>
</table>
</section>
<section id="notes">
<h2>Notes<a class="headerlink" href="#notes" title="Link to this heading"></a></h2>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="id54" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></span>
<span class="backrefs">(<a role="doc-backlink" href="#id1">1</a>,<a role="doc-backlink" href="#id2">2</a>,<a role="doc-backlink" href="#id4">3</a>,<a role="doc-backlink" href="#id7">4</a>,<a role="doc-backlink" href="#id8">5</a>,<a role="doc-backlink" href="#id15">6</a>,<a role="doc-backlink" href="#id16">7</a>,<a role="doc-backlink" href="#id23">8</a>,<a role="doc-backlink" href="#id25">9</a>,<a role="doc-backlink" href="#id29">10</a>,<a role="doc-backlink" href="#id34">11</a>,<a role="doc-backlink" href="#id38">12</a>)</span>
<p>Amortized. An individual operation may occasionally be <em>O</em>(<em>n</em>)
when the underlying storage is resized, but this cost is spread over
many operations, depending on the history of the container.</p>
</aside>
<aside class="footnote brackets" id="id55" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></span>
<span class="backrefs">(<a role="doc-backlink" href="#id3">1</a>,<a role="doc-backlink" href="#id5">2</a>,<a role="doc-backlink" href="#id6">3</a>)</span>
<p>Popping or deleting the element at index <em>k</em> of a list of size <em>n</em>
shifts all elements after <em>k</em> one slot to the left, moving <em>n</em> - <em>k</em> - 1
elements; inserting at index <em>k</em> shifts the elements from <em>k</em> onwards one
slot to the right, moving <em>n</em> - <em>k</em> elements. The worst case is index 0,
where the whole rest of the list has to be moved; the average case, an
index in the middle of the list, takes <em>O</em>(<em>n</em>/2) = <em>O</em>(<em>n</em>)
operations; and operating at the end of the list moves nothing and is
<em>O</em>(1).</p>
</aside>
<aside class="footnote brackets" id="id56" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>3<span class="fn-bracket">]</span></span>
<span class="backrefs">(<a role="doc-backlink" href="#id9">1</a>,<a role="doc-backlink" href="#id17">2</a>)</span>
<p>Plus the cost of iterating over <em>t</em>, which may be expensive for an
arbitrary iterable.</p>
</aside>
<aside class="footnote brackets" id="id57" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id10">4</a><span class="fn-bracket">]</span></span>
<p>This is the worst case scenario. Sorting is adaptive and input that is
already sorted or reverse-sorted takes only <em>O</em>(<em>n</em>) comparisons.
See <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/3.15/Objects/listsort.txt">Objects/listsort.txt</a> for more information.</p>
</aside>
<aside class="footnote brackets" id="id58" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>5<span class="fn-bracket">]</span></span>
<span class="backrefs">(<a role="doc-backlink" href="#id11">1</a>,<a role="doc-backlink" href="#id12">2</a>,<a role="doc-backlink" href="#id20">3</a>,<a role="doc-backlink" href="#id40">4</a>,<a role="doc-backlink" href="#id46">5</a>,<a role="doc-backlink" href="#id50">6</a>,<a role="doc-backlink" href="#id53">7</a>)</span>
<p>The number of elements is stored in the object, so <code class="docutils literal notranslate"><span class="pre">len()</span></code> does
not need to count them.</p>
</aside>
<aside class="footnote brackets" id="id59" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>6<span class="fn-bracket">]</span></span>
<span class="backrefs">(<a role="doc-backlink" href="#id13">1</a>,<a role="doc-backlink" href="#id21">2</a>)</span>
<p>Copying a <a class="reference internal" href="stdtypes.html#frozendict" title="frozendict"><code class="xref py py-class docutils literal notranslate"><span class="pre">frozendict</span></code></a> or a <a class="reference internal" href="stdtypes.html#frozenset" title="frozenset"><code class="xref py py-class docutils literal notranslate"><span class="pre">frozenset</span></code></a> is <em>O</em>(1) as it
returns the original object.</p>
</aside>
<aside class="footnote brackets" id="id60" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></span>
<span class="backrefs">(<a role="doc-backlink" href="#id14">1</a>,<a role="doc-backlink" href="#id18">2</a>,<a role="doc-backlink" href="#id19">3</a>,<a role="doc-backlink" href="#id22">4</a>,<a role="doc-backlink" href="#id24">5</a>,<a role="doc-backlink" href="#id26">6</a>,<a role="doc-backlink" href="#id27">7</a>,<a role="doc-backlink" href="#id30">8</a>,<a role="doc-backlink" href="#id32">9</a>,<a role="doc-backlink" href="#id35">10</a>,<a role="doc-backlink" href="#id37">11</a>,<a role="doc-backlink" href="#id39">12</a>)</span>
<p>These operations scan the container’s internal hash table, which is
not shrunk when elements are removed. After removing most elements, they
still take time proportional to the container’s former size, until a
later insertion triggers a resize.</p>
</aside>
<aside class="footnote brackets" id="id61" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>8<span class="fn-bracket">]</span></span>
<span class="backrefs">(<a role="doc-backlink" href="#id28">1</a>,<a role="doc-backlink" href="#id31">2</a>,<a role="doc-backlink" href="#id36">3</a>)</span>
<p><em>O</em>(len(<em>t</em>)) if <em>t</em> is not a set.</p>
</aside>
<aside class="footnote brackets" id="id62" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id33">9</a><span class="fn-bracket">]</span></span>
<p><em>O</em>(len(<em>s</em>) + len(<em>t</em>)) if <em>t</em> is not a set.</p>
</aside>
<aside class="footnote brackets" id="id63" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id41">10</a><span class="fn-bracket">]</span></span>
<p>Each concatenation builds a new object, so building a string by
concatenating many pieces in a loop is quadratic in the total length.
See the <a class="reference internal" href="stdtypes.html#typesseq-repeated-concatenation"><span class="std std-ref">note on concatenating immutable sequences</span></a> for alternatives.</p>
</aside>
<aside class="footnote brackets" id="id64" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>11<span class="fn-bracket">]</span></span>
<span class="backrefs">(<a role="doc-backlink" href="#id42">1</a>,<a role="doc-backlink" href="#id43">2</a>,<a role="doc-backlink" href="#id47">3</a>)</span>
<p>With <em>start</em> and <em>end</em> arguments, <em>n</em> is the length of the region
searched rather than of <em>s</em>, and unlike slicing nothing is copied.</p>
</aside>
<aside class="footnote brackets" id="id65" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id44">12</a><span class="fn-bracket">]</span></span>
<p>This is the worst case. Reverse searches are <em>O</em>(<em>n</em>) on typical
input. Forward searches instead use a more elaborate algorithm with a
linear worst case, described in
<a class="extlink-source reference external" href="https://github.com/python/cpython/tree/3.15/Objects/stringlib/stringlib_find_two_way_notes.txt">Objects/stringlib/stringlib_find_two_way_notes.txt</a>.</p>
</aside>
<aside class="footnote brackets" id="id66" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id45">13</a><span class="fn-bracket">]</span></span>
<p>This assumes a codec that does a constant amount of work per character.</p>
</aside>
<aside class="footnote brackets" id="id67" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>14<span class="fn-bracket">]</span></span>
<span class="backrefs">(<a role="doc-backlink" href="#id48">1</a>,<a role="doc-backlink" href="#id49">2</a>)</span>
<p>These unpack and compare each element individually, so they are much
slower than the equivalent <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> methods.</p>
</aside>
<aside class="footnote brackets" id="id68" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>15<span class="fn-bracket">]</span></span>
<span class="backrefs">(<a role="doc-backlink" href="#id51">1</a>,<a role="doc-backlink" href="#id52">2</a>)</span>
<p>Assuming <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> or <a class="reference internal" href="functions.html#bool" title="bool"><code class="xref py py-class docutils literal notranslate"><span class="pre">bool</span></code></a> arguments. For other types,
the range is searched like any other sequence in <em>O</em>(<em>n</em>) time.</p>
</aside>
</aside>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<div>
<h3><a href="../contents.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Time complexity of operations on built-in types</a><ul>
<li><a class="reference internal" href="#list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a></li>
<li><a class="reference internal" href="#tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a></li>
<li><a class="reference internal" href="#dict-frozendict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">frozendict</span></code></a></li>
<li><a class="reference internal" href="#set-frozenset"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">frozenset</span></code></a></li>
<li><a class="reference internal" href="#str-bytes-bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a></li>
<li><a class="reference internal" href="#memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a></li>
<li><a class="reference internal" href="#range"><code class="xref py py-class docutils literal notranslate"><span class="pre">range</span></code></a></li>
<li><a class="reference internal" href="#notes">Notes</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Previous topic</h4>
<p class="topless"><a href="threadsafety.html"
title="previous chapter">Thread Safety Guarantees</a></p>
</div>
<div>
<h4>Next topic</h4>
<p class="topless"><a href="../library/index.html"
title="next chapter">The Python standard library</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "builtins/time-complexity.rst");
element.href = url.toString();
});
});
</script>
<div role="note" aria-label="source link">
<h3>This page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a bug</a></li>
<li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.15/Doc/builtins/time-complexity.rst?plain=1"
rel="nofollow">Show source
</a>
</li>
</ul>
</div>
</div>
<div id="sidebarbutton" title="Collapse sidebar">
<span>«</span>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="../library/index.html" title="The Python standard library"
>next</a> |</li>
<li class="right" >
<a href="threadsafety.html" title="Thread Safety Guarantees"
>previous</a> |</li>
<li class="root-icon"><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.15.0 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python built-ins reference</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">Time complexity of operations on built-in types</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">
<input type="submit" value="Go">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.
<br>
This page is licensed under the Python Software Foundation License Version 2.
<br>
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br>
See <a href="/license.html">History and License</a> for more information.<br>
<br>
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br>
<br>
Last updated on Oct 09, 2026 (11:35 UTC).
<a href="/bugs.html">Found a bug</a>?
<br>
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.
</div>
</body>
</html>