mirror of
https://github.com/rasbt/python_reference.git
synced 2024-11-23 20:11:13 +00:00
section about funcs that dont return lists anymore
This commit is contained in:
parent
ea23193787
commit
663a0c2398
|
@ -1762,6 +1762,7 @@ document.write('<a h'+'ref'+'="ma'+'ilto'+':'+e+'">'+'email'+'<\/'+'a'+'>');
|
|||
<li><p><a href="#For-loop-variables-and-the-global-namespace-leak">For-loop variables and the global namespace leak</a></p></li>
|
||||
<li><p><a href="#Comparing-unorderable-types">Comparing unorderable types</a></p></li>
|
||||
<li><p><a href="#Parsing-user-inputs-via-input">Parsing user inputs via input()</a></p></li>
|
||||
<li><p><a href="#Returning-iterable-objects-instead-of-lists">Returning iterable objects instead of lists</a></p></li>
|
||||
<li><p><a href="#More-articles-about-Python-2-and-Python-3">More articles about Python 2 and Python 3</a></p></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
@ -4006,6 +4007,163 @@ enter a number: <span style="color: #0000DD; font-weight: bold">123</span>
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h2 id="Returning-iterable-objects-instead-of-lists">Returning iterable objects instead of lists<a class="anchor-link" href="#Returning-iterable-objects-instead-of-lists">¶</a></h2>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<p>[<a href="#Sections">back to the section-overview</a>]</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<p>As we have already seen in the <a href="#xrange"><code>xrange</code></a> section, some functions and methods return iterable objects in Python 3 now - instead of lists in Python 2.</p>
|
||||
<p>Since we usually iterate over those only once anyway, I think this change makes a lot of sense to save memory. However, it is also possible - in contrast to generators - to iterate over those multiple times if needed, it is aonly not so efficient.</p>
|
||||
<p>And for those cases where we really need the <code>list</code>-objects, we can simply convert the iterable object into a <code>list</code> via the <code>list()</code> function.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h4 id="python-2">Python 2</h4>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing code_cell rendered">
|
||||
<div class="input">
|
||||
<div class="prompt input_prompt">
|
||||
In [2]:
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class="highlight"><pre><span class="k">print</span> <span class="s">'Python'</span><span class="p">,</span> <span class="n">python_version</span><span class="p">()</span>
|
||||
|
||||
<span class="k">print</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
|
||||
<span class="k">print</span> <span class="nb">type</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
|
||||
</pre></div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="output_wrapper">
|
||||
<div class="output">
|
||||
|
||||
|
||||
<div class="output_area"><div class="prompt"></div>
|
||||
<div class="output_subarea output_stream output_stdout output_text">
|
||||
<pre>
|
||||
Python 2.7.6
|
||||
[0, 1, 2]
|
||||
<type 'list'>
|
||||
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h4 id="python-3">Python 3</h4>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing code_cell rendered">
|
||||
<div class="input">
|
||||
<div class="prompt input_prompt">
|
||||
In [7]:
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class="highlight"><pre><span class="k">print</span><span class="p">(</span><span class="s">'Python'</span><span class="p">,</span> <span class="n">python_version</span><span class="p">())</span>
|
||||
|
||||
<span class="k">print</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">)))</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">)))</span>
|
||||
</pre></div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="output_wrapper">
|
||||
<div class="output">
|
||||
|
||||
|
||||
<div class="output_area"><div class="prompt"></div>
|
||||
<div class="output_subarea output_stream output_stdout output_text">
|
||||
<pre>
|
||||
Python 3.4.1
|
||||
range(0, 3)
|
||||
<class 'range'>
|
||||
[0, 1, 2]
|
||||
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<p><br></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<p><strong>Some more commonly used functions and methods that don't return lists anymore in Python 3:</strong></p>
|
||||
<ul>
|
||||
<li><p><code>zip()</code></p></li>
|
||||
<li><p><code>map()</code></p></li>
|
||||
<li><p><code>filter()</code></p></li>
|
||||
<li><p>dictionary's <code>.keys()</code> method</p></li>
|
||||
<li><p>dictionary's <code>.values()</code> method</p></li>
|
||||
<li><p>dictionary's <code>.items()</code> method</p></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<p><br> <br></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h2 id="More-articles-about-Python-2-and-Python-3">More articles about Python 2 and Python 3<a class="anchor-link" href="#More-articles-about-Python-2-and-Python-3">¶</a></h2>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:eb93a4258ce700755924939b5dc873c027a656b1faafcd0228616b8575378418"
|
||||
"signature": "sha256:969dffb51ccca00a6136f4187304ff4a29444b347d9c38f9fe33f77a4af5f6fe"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -95,6 +95,8 @@
|
|||
"\n",
|
||||
"- [Parsing user inputs via input()](#Parsing-user-inputs-via-input)\n",
|
||||
"\n",
|
||||
"- [Returning iterable objects instead of lists](#Returning-iterable-objects-instead-of-lists)\n",
|
||||
"\n",
|
||||
"- [More articles about Python 2 and Python 3](#More-articles-about-Python-2-and-Python-3)"
|
||||
]
|
||||
},
|
||||
|
@ -1721,6 +1723,130 @@
|
|||
"<br>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "heading",
|
||||
"level": 2,
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Returning iterable objects instead of lists"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"[[back to the section-overview](#Sections)]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"As we have already seen in the [`xrange`](#xrange) section, some functions and methods return iterable objects in Python 3 now - instead of lists in Python 2. \n",
|
||||
"\n",
|
||||
"Since we usually iterate over those only once anyway, I think this change makes a lot of sense to save memory. However, it is also possible - in contrast to generators - to iterate over those multiple times if needed, it is aonly not so efficient.\n",
|
||||
"\n",
|
||||
"And for those cases where we really need the `list`-objects, we can simply convert the iterable object into a `list` via the `list()` function."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Python 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"print 'Python', python_version() \n",
|
||||
"\n",
|
||||
"print range(3) \n",
|
||||
"print type(range(3))"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"Python 2.7.6\n",
|
||||
"[0, 1, 2]\n",
|
||||
"<type 'list'>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 2
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Python 3"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"print('Python', python_version())\n",
|
||||
"\n",
|
||||
"print(range(3))\n",
|
||||
"print(type(range(3)))\n",
|
||||
"print(list(range(3)))"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"Python 3.4.1\n",
|
||||
"range(0, 3)\n",
|
||||
"<class 'range'>\n",
|
||||
"[0, 1, 2]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 7
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<br>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Some more commonly used functions and methods that don't return lists anymore in Python 3:**\n",
|
||||
"\n",
|
||||
"- `zip()`\n",
|
||||
"\n",
|
||||
"- `map()`\n",
|
||||
"\n",
|
||||
"- `filter()`\n",
|
||||
"\n",
|
||||
"- dictionary's `.keys()` method\n",
|
||||
"\n",
|
||||
"- dictionary's `.values()` method\n",
|
||||
"\n",
|
||||
"- dictionary's `.items()` method\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<br>\n",
|
||||
"<br>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "heading",
|
||||
"level": 2,
|
||||
|
|
Loading…
Reference in New Issue
Block a user