Skip to content

Commit 663a0c2

Browse files
committed
section about funcs that dont return lists anymore
1 parent ea23193 commit 663a0c2

2 files changed

Lines changed: 285 additions & 1 deletion

File tree

tutorials/key_differences_between_python_2_and_3.html

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,7 @@ <h2 id="Sections">Sections<a class="anchor-link" href="#Sections">&#182;</a></h2
17621762
<li><p><a href="#For-loop-variables-and-the-global-namespace-leak">For-loop variables and the global namespace leak</a></p></li>
17631763
<li><p><a href="#Comparing-unorderable-types">Comparing unorderable types</a></p></li>
17641764
<li><p><a href="#Parsing-user-inputs-via-input">Parsing user inputs via input()</a></p></li>
1765+
<li><p><a href="#Returning-iterable-objects-instead-of-lists">Returning iterable objects instead of lists</a></p></li>
17651766
<li><p><a href="#More-articles-about-Python-2-and-Python-3">More articles about Python 2 and Python 3</a></p></li>
17661767
</ul>
17671768
</div>
@@ -4006,6 +4007,163 @@ <h4 id="python-3">Python 3</h4>
40064007
</div>
40074008
<div class="inner_cell">
40084009
<div class="text_cell_render border-box-sizing rendered_html">
4010+
<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">&#182;</a></h2>
4011+
</div>
4012+
</div>
4013+
</div>
4014+
4015+
<div class="cell border-box-sizing text_cell rendered">
4016+
<div class="prompt input_prompt">
4017+
</div>
4018+
<div class="inner_cell">
4019+
<div class="text_cell_render border-box-sizing rendered_html">
4020+
<p>[<a href="#Sections">back to the section-overview</a>]</p>
4021+
</div>
4022+
</div>
4023+
</div>
4024+
<div class="cell border-box-sizing text_cell rendered">
4025+
<div class="prompt input_prompt">
4026+
</div>
4027+
<div class="inner_cell">
4028+
<div class="text_cell_render border-box-sizing rendered_html">
4029+
<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>
4030+
<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>
4031+
<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>
4032+
</div>
4033+
</div>
4034+
</div>
4035+
<div class="cell border-box-sizing text_cell rendered">
4036+
<div class="prompt input_prompt">
4037+
</div>
4038+
<div class="inner_cell">
4039+
<div class="text_cell_render border-box-sizing rendered_html">
4040+
<h4 id="python-2">Python 2</h4>
4041+
</div>
4042+
</div>
4043+
</div>
4044+
<div class="cell border-box-sizing code_cell rendered">
4045+
<div class="input">
4046+
<div class="prompt input_prompt">
4047+
In&nbsp;[2]:
4048+
</div>
4049+
<div class="inner_cell">
4050+
<div class="input_area">
4051+
<div class="highlight"><pre><span class="k">print</span> <span class="s">&#39;Python&#39;</span><span class="p">,</span> <span class="n">python_version</span><span class="p">()</span>
4052+
4053+
<span class="k">print</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
4054+
<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>
4055+
</pre></div>
4056+
4057+
</div>
4058+
</div>
4059+
</div>
4060+
4061+
<div class="output_wrapper">
4062+
<div class="output">
4063+
4064+
4065+
<div class="output_area"><div class="prompt"></div>
4066+
<div class="output_subarea output_stream output_stdout output_text">
4067+
<pre>
4068+
Python 2.7.6
4069+
[0, 1, 2]
4070+
&lt;type &apos;list&apos;&gt;
4071+
4072+
</pre>
4073+
</div>
4074+
</div>
4075+
4076+
</div>
4077+
</div>
4078+
4079+
</div>
4080+
<div class="cell border-box-sizing text_cell rendered">
4081+
<div class="prompt input_prompt">
4082+
</div>
4083+
<div class="inner_cell">
4084+
<div class="text_cell_render border-box-sizing rendered_html">
4085+
<h4 id="python-3">Python 3</h4>
4086+
</div>
4087+
</div>
4088+
</div>
4089+
<div class="cell border-box-sizing code_cell rendered">
4090+
<div class="input">
4091+
<div class="prompt input_prompt">
4092+
In&nbsp;[7]:
4093+
</div>
4094+
<div class="inner_cell">
4095+
<div class="input_area">
4096+
<div class="highlight"><pre><span class="k">print</span><span class="p">(</span><span class="s">&#39;Python&#39;</span><span class="p">,</span> <span class="n">python_version</span><span class="p">())</span>
4097+
4098+
<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>
4099+
<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>
4100+
<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>
4101+
</pre></div>
4102+
4103+
</div>
4104+
</div>
4105+
</div>
4106+
4107+
<div class="output_wrapper">
4108+
<div class="output">
4109+
4110+
4111+
<div class="output_area"><div class="prompt"></div>
4112+
<div class="output_subarea output_stream output_stdout output_text">
4113+
<pre>
4114+
Python 3.4.1
4115+
range(0, 3)
4116+
&lt;class &apos;range&apos;&gt;
4117+
[0, 1, 2]
4118+
4119+
</pre>
4120+
</div>
4121+
</div>
4122+
4123+
</div>
4124+
</div>
4125+
4126+
</div>
4127+
<div class="cell border-box-sizing text_cell rendered">
4128+
<div class="prompt input_prompt">
4129+
</div>
4130+
<div class="inner_cell">
4131+
<div class="text_cell_render border-box-sizing rendered_html">
4132+
<p><br></p>
4133+
</div>
4134+
</div>
4135+
</div>
4136+
<div class="cell border-box-sizing text_cell rendered">
4137+
<div class="prompt input_prompt">
4138+
</div>
4139+
<div class="inner_cell">
4140+
<div class="text_cell_render border-box-sizing rendered_html">
4141+
<p><strong>Some more commonly used functions and methods that don't return lists anymore in Python 3:</strong></p>
4142+
<ul>
4143+
<li><p><code>zip()</code></p></li>
4144+
<li><p><code>map()</code></p></li>
4145+
<li><p><code>filter()</code></p></li>
4146+
<li><p>dictionary's <code>.keys()</code> method</p></li>
4147+
<li><p>dictionary's <code>.values()</code> method</p></li>
4148+
<li><p>dictionary's <code>.items()</code> method</p></li>
4149+
</ul>
4150+
</div>
4151+
</div>
4152+
</div>
4153+
<div class="cell border-box-sizing text_cell rendered">
4154+
<div class="prompt input_prompt">
4155+
</div>
4156+
<div class="inner_cell">
4157+
<div class="text_cell_render border-box-sizing rendered_html">
4158+
<p><br> <br></p>
4159+
</div>
4160+
</div>
4161+
</div>
4162+
<div class="cell border-box-sizing text_cell rendered">
4163+
<div class="prompt input_prompt">
4164+
</div>
4165+
<div class="inner_cell">
4166+
<div class="text_cell_render border-box-sizing rendered_html">
40094167
<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">&#182;</a></h2>
40104168
</div>
40114169
</div>

tutorials/key_differences_between_python_2_and_3.ipynb

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:eb93a4258ce700755924939b5dc873c027a656b1faafcd0228616b8575378418"
4+
"signature": "sha256:969dffb51ccca00a6136f4187304ff4a29444b347d9c38f9fe33f77a4af5f6fe"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -95,6 +95,8 @@
9595
"\n",
9696
"- [Parsing user inputs via input()](#Parsing-user-inputs-via-input)\n",
9797
"\n",
98+
"- [Returning iterable objects instead of lists](#Returning-iterable-objects-instead-of-lists)\n",
99+
"\n",
98100
"- [More articles about Python 2 and Python 3](#More-articles-about-Python-2-and-Python-3)"
99101
]
100102
},
@@ -1721,6 +1723,130 @@
17211723
"<br>"
17221724
]
17231725
},
1726+
{
1727+
"cell_type": "heading",
1728+
"level": 2,
1729+
"metadata": {},
1730+
"source": [
1731+
"Returning iterable objects instead of lists"
1732+
]
1733+
},
1734+
{
1735+
"cell_type": "markdown",
1736+
"metadata": {},
1737+
"source": [
1738+
"[[back to the section-overview](#Sections)]"
1739+
]
1740+
},
1741+
{
1742+
"cell_type": "markdown",
1743+
"metadata": {},
1744+
"source": [
1745+
"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",
1746+
"\n",
1747+
"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",
1748+
"\n",
1749+
"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."
1750+
]
1751+
},
1752+
{
1753+
"cell_type": "markdown",
1754+
"metadata": {},
1755+
"source": [
1756+
"#### Python 2"
1757+
]
1758+
},
1759+
{
1760+
"cell_type": "code",
1761+
"collapsed": false,
1762+
"input": [
1763+
"print 'Python', python_version() \n",
1764+
"\n",
1765+
"print range(3) \n",
1766+
"print type(range(3))"
1767+
],
1768+
"language": "python",
1769+
"metadata": {},
1770+
"outputs": [
1771+
{
1772+
"output_type": "stream",
1773+
"stream": "stdout",
1774+
"text": [
1775+
"Python 2.7.6\n",
1776+
"[0, 1, 2]\n",
1777+
"<type 'list'>\n"
1778+
]
1779+
}
1780+
],
1781+
"prompt_number": 2
1782+
},
1783+
{
1784+
"cell_type": "markdown",
1785+
"metadata": {},
1786+
"source": [
1787+
"#### Python 3"
1788+
]
1789+
},
1790+
{
1791+
"cell_type": "code",
1792+
"collapsed": false,
1793+
"input": [
1794+
"print('Python', python_version())\n",
1795+
"\n",
1796+
"print(range(3))\n",
1797+
"print(type(range(3)))\n",
1798+
"print(list(range(3)))"
1799+
],
1800+
"language": "python",
1801+
"metadata": {},
1802+
"outputs": [
1803+
{
1804+
"output_type": "stream",
1805+
"stream": "stdout",
1806+
"text": [
1807+
"Python 3.4.1\n",
1808+
"range(0, 3)\n",
1809+
"<class 'range'>\n",
1810+
"[0, 1, 2]\n"
1811+
]
1812+
}
1813+
],
1814+
"prompt_number": 7
1815+
},
1816+
{
1817+
"cell_type": "markdown",
1818+
"metadata": {},
1819+
"source": [
1820+
"<br>"
1821+
]
1822+
},
1823+
{
1824+
"cell_type": "markdown",
1825+
"metadata": {},
1826+
"source": [
1827+
"**Some more commonly used functions and methods that don't return lists anymore in Python 3:**\n",
1828+
"\n",
1829+
"- `zip()`\n",
1830+
"\n",
1831+
"- `map()`\n",
1832+
"\n",
1833+
"- `filter()`\n",
1834+
"\n",
1835+
"- dictionary's `.keys()` method\n",
1836+
"\n",
1837+
"- dictionary's `.values()` method\n",
1838+
"\n",
1839+
"- dictionary's `.items()` method\n"
1840+
]
1841+
},
1842+
{
1843+
"cell_type": "markdown",
1844+
"metadata": {},
1845+
"source": [
1846+
"<br>\n",
1847+
"<br>"
1848+
]
1849+
},
17241850
{
17251851
"cell_type": "heading",
17261852
"level": 2,

0 commit comments

Comments
 (0)