is it a difference between del, remove and pop for lists? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
3rd May 2018, 12:08 AM
Mohammad
Mohammad - avatar
2 Answers
+ 3
x = [1,2,3,4,5,6,7,8,9,10]; print (dir(x)); #print (help(x.remove)); #print (help(x.pop)); print (x); ''' remove(...) method of builtins.list instance L.remove(value) -> None -- remove first occurrence of value. Raises ValueError if the value is not present. ''' x.remove(4); print (x); ''' pop(...) method of builtins.list instance L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range. ''' print (x.pop(2)); print (x);
4th May 2018, 4:40 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 3
del removes the item at a specific index. Example: x = [3, 2, 2, 1] del x[1]
4th May 2018, 4:42 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar