開始使用免費開始

刪除串列元素

最後,你還可以從串列中刪除元素。使用 del 陳述式即可完成:

x = ["a", "b", "c", "d"]
del x[1]

注意:一旦從串列中刪除某個元素,該元素後所有元素的索引都會改變!

可惜的是,你中樂透的獎金並沒有想像中那麼多,看來蓋泳池小屋的計畫要落空了。你需要把它從串列中刪除。因此,你決定從 areas 串列中刪除對應的字串和浮點數。

本練習屬於課程

Python 入門

檢視課程

練習說明

  • 從你的 areas 串列中刪除 "poolhouse" 對應的字串和浮點數。
  • 印出更新後的 areas 串列。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

areas = ["hallway", 11.25, "kitchen", 18.0,
        "chill zone", 20.0, "bedroom", 10.75,
         "bathroom", 10.50, "poolhouse", 24.5,
         "garage", 15.45]

# Delete the poolhouse items from the list


# Print the updated list
編輯並執行程式碼