diff --git a/tutorials/key_differences_between_python_2_and_3.html b/tutorials/key_differences_between_python_2_and_3.html index 74d5094..94ddb8e 100644 --- a/tutorials/key_differences_between_python_2_and_3.html +++ b/tutorials/key_differences_between_python_2_and_3.html @@ -1762,6 +1762,7 @@ document.write(''+'email'+'<\/'+'a'+'>');
  • For-loop variables and the global namespace leak

  • Comparing unorderable types

  • Parsing user inputs via input()

  • +
  • Returning iterable objects instead of lists

  • More articles about Python 2 and Python 3

  • @@ -4006,6 +4007,163 @@ enter a number: 123
    +

    Returning iterable objects instead of lists

    +
    +
    + + +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    +
    +

    As we have already seen in the xrange section, some functions and methods return iterable objects in Python 3 now - instead of lists in Python 2.

    +

    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.

    +

    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.

    +
    +
    +
    +
    +
    +
    +
    +
    +

    Python 2

    +
    +
    +
    +
    +
    +
    +In [2]: +
    +
    +
    +
    print 'Python', python_version() 
    +
    +print range(3) 
    +print type(range(3))
    +
    + +
    +
    +
    + +
    +
    + + +
    +
    +
    +Python 2.7.6
    +[0, 1, 2]
    +<type 'list'>
    +
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    +
    +
    +

    Python 3

    +
    +
    +
    +
    +
    +
    +In [7]: +
    +
    +
    +
    print('Python', python_version())
    +
    +print(range(3))
    +print(type(range(3)))
    +print(list(range(3)))
    +
    + +
    +
    +
    + +
    +
    + + +
    +
    +
    +Python 3.4.1
    +range(0, 3)
    +<class 'range'>
    +[0, 1, 2]
    +
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    +
    +
    +


    +
    +
    +
    +
    +
    +
    +
    +
    +

    Some more commonly used functions and methods that don't return lists anymore in Python 3:

    +
      +
    • zip()

    • +
    • map()

    • +
    • filter()

    • +
    • dictionary's .keys() method

    • +
    • dictionary's .values() method

    • +
    • dictionary's .items() method

    • +
    +
    +
    +
    +
    +
    +
    +
    +
    +



    +
    +
    +
    +
    +
    +
    +
    +
    diff --git a/tutorials/key_differences_between_python_2_and_3.ipynb b/tutorials/key_differences_between_python_2_and_3.ipynb index 8b2577a..bfc2af3 100644 --- a/tutorials/key_differences_between_python_2_and_3.ipynb +++ b/tutorials/key_differences_between_python_2_and_3.ipynb @@ -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 @@ "
    " ] }, + { + "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", + "\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", + "\n", + "[0, 1, 2]\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
    " + ] + }, + { + "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": [ + "
    \n", + "
    " + ] + }, { "cell_type": "heading", "level": 2,