diff --git a/lists.ipynb b/lists.ipynb index 28ed228..8b31fb6 100644 --- a/lists.ipynb +++ b/lists.ipynb @@ -510,6 +510,56 @@ "\n", "Complete Exercise 3-8 in Matthes (2023, p. 45)." ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Store the locations in a list (not in alphabetical order)\n", + "places_to_visit = [\"Japan\", \"Norway\", \"Iceland\", \"Austria\", \"South Africa\"]\n", + "\n", + "# Print the list in its original order\n", + "print(\"Original list:\")\n", + "print(places_to_visit)\n", + "\n", + "# Print the list in alphabetical order without modifying it\n", + "print(\"\\nAlphabetical order (using sorted()):\")\n", + "print(sorted(places_to_visit))\n", + "\n", + "# Show the list is still in its original order\n", + "print(\"\\nList after sorted() (still original order):\")\n", + "print(places_to_visit)\n", + "\n", + "# Print the list in reverse-alphabetical order without modifying it\n", + "print(\"\\nReverse alphabetical order (using sorted() with reverse=True):\")\n", + "print(sorted(places_to_visit, reverse=True))\n", + "\n", + "# Show the list is still in its original order\n", + "print(\"\\nList after sorted() with reverse=True (still original order):\")\n", + "print(places_to_visit)\n", + "\n", + "# Use reverse() to change the order of the list\n", + "places_to_visit.reverse()\n", + "print(\"\\nList after reverse():\")\n", + "print(places_to_visit)\n", + "\n", + "# Use reverse() to return to the original order\n", + "places_to_visit.reverse()\n", + "print(\"\\nList after second reverse() (back to original order):\")\n", + "print(places_to_visit)\n", + "\n", + "# Use sort() to change the list to alphabetical order\n", + "places_to_visit.sort()\n", + "print(\"\\nList after sort() (alphabetical order):\")\n", + "print(places_to_visit)\n", + "\n", + "# Use sort() to change the list to reverse alphabetical order\n", + "places_to_visit.sort(reverse=True)\n", + "print(\"\\nList after sort(reverse=True) (reverse alphabetical order):\")\n", + "print(places_to_visit)\n" + ] } ], "metadata": {