diff --git a/part-4.ipynb b/part-4.ipynb index 385d7f9..5a5e269 100644 --- a/part-4.ipynb +++ b/part-4.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:40f96dd4a9a68767b80bf16809c2b3aebd08f6893eb2d01994fd18e611864f26" + "signature": "sha256:d806710bd05c6f416dc3842515677b9246aa116881cfd0f7dd2c6b748a3a67e5" }, "nbformat": 3, "nbformat_minor": 0, @@ -110,13 +110,44 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Note how our list comprehension is written within the brackets: The for-loop statement is written at the end whereas the action inside of the for-loop is written first: n * n" + "Let's compare the two methods real quick by highlighting the different logical parts." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "First written as a conventional 'for' loop:\n", + "
\n",
+      "squared_numbers = []\n",
+      "for n in my_favorite_numbers:\n",
+      "    squared_numbers.append(n * n)\n",
+      "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And then written as a list comprehension:\n", + "
\n",
+      "squared_numbers = [n * n for n in my_favorite_numbers]\n",
+      "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Look for the new list being created, the iteration being done over the orignal list and the transformation being done on each element. List comprehensions are just a cleaner way of building lists from other iterables." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Improving our work\n", + "\n", "Let's revisit a problem we've already solved in Danny's lecture on list:\n", "\n", "Pick every name from a list that begins with a vowel.\n",