0

Help

How can I delete an item from a list in py

21st Jan 2020, 11:55 PM
Tovi Ng
Tovi Ng - avatar
2 Antworten
+ 4
There are a couple of different functions to do this. You can use pop(), remove(), and del. pop() removes an item from the given list by index number. For example: num = [1, 2, 3, 4, 5] num.pop(2) #returns 3, which is at index 2, and removes 3 from list remove(), on the hand, removes an item by value from a list. For example: num = [1, 2, 3, 4, 5] num.remove(3) #removes the value 3, meaning, the final list is [1, 2, 4, 5] Finally, del removes a item from a list similar to pop(). All you have to do is: del num[2] #does the same thing as num.pop(2)
22nd Jan 2020, 12:07 AM
Jianmin Chen
Jianmin Chen - avatar
+ 2
for example this removes the fifth element: del your_list[5] And this removes the first find of the item 'candy': your_list.remove('candy') And then you can just grab out the last element (it will be returned): your_list.pop()
22nd Jan 2020, 12:06 AM
HonFu
HonFu - avatar