1. \u00a0List 10 places\nplaces = ["Paris", "Tokyo", "New York City", "Rome", "Sydney", "London", "Barcelona", "Rio de Janeiro", "Cape Town", "Bali"]\n\n2) Reverse the list\nreverse_places = places[::-1]\nprint(reverse_places)\n\n3) Sort the list in ascending order\nsorted_places = sorted(places)\nprint(sorted_places)\n\n4) Sort the list in descending order\nreverse_sorted_places = sorted(places, reverse=True)\nprint(reverse_sorted_places)\n\n5) Add "Antarctic" to the beginning and "Arctic Sea" to the end of the list\nplaces.insert(0, "Antarctic")\nplaces.append("Arctic Sea")\nprint(places)\n\n6) Insert "Chicago" at the middle of the list\nmiddle_index = len(places) // 2\nplaces.insert(middle_index, "Chicago")\nprint(places)\n\n7) Delete the 3rd and 9th elements from the list\ndel places[2]\ndel places[8]\nprint(places)
Python List Manipulation: Adding, Removing, and Sorting Elements - Comprehensive Guide

原文地址: https://www.cveoy.top/t/topic/pnuc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录