Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lists.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down