pop()將列表指定位置的元素移除,同時可以將移除的元素賦值給某個變量,不填寫位置參數則默認刪除最后一位
pop()根據鍵將字典中指定的鍵值對刪除,同時可以將刪除的值賦值給變量
舉個例子:
1 a = ["hello", "world", "dlrb"] 2 b = ["hello", "world", "dlrb"] 3 a.pop(1) 4 b1 = b.pop(0) 5 print(a) 6 print(b1)
輸出結果:
['hello', 'dlrb'] hello
我們將列表a的位置1的元素移除
將列表b的位置0的元素移除並賦值給變量b1
1 b = { 2 "name":"dlrb", 3 "age":25, 4 "height":168 5 } 6 b1 = b.pop("age") 7 print(b) 8 print(b1)
輸出結果:
{'name': 'dlrb', 'height': 168} 25