You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>As we have already seen in the <ahref="#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>
<h2id="More-articles-about-Python-2-and-Python-3">More articles about Python 2 and Python 3<aclass="anchor-link" href="#More-articles-about-Python-2-and-Python-3">¶</a></h2>
"- [Parsing user inputs via input()](#Parsing-user-inputs-via-input)\n",
97
97
"\n",
98
+
"- [Returning iterable objects instead of lists](#Returning-iterable-objects-instead-of-lists)\n",
99
+
"\n",
98
100
"- [More articles about Python 2 and Python 3](#More-articles-about-Python-2-and-Python-3)"
99
101
]
100
102
},
@@ -1721,6 +1723,130 @@
1721
1723
"<br>"
1722
1724
]
1723
1725
},
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",
0 commit comments