![]() ![]() We use del() method to delete an item not to return any value. In this case, the last element is ‘Hindi’. And when no index value is given, then the last element is popped out. In the above example, we did not passed an index value. If we do not provide any index value then language = In the last line of the code we printed the updated list. And then we printed the returned value i.e ‘urdu’. In this case, the element we popped is at index 4. In the above example, we used the pop() function to pop an element at a specified index. If no parameter is passed, the default index -1 is taken and it returns the last item from the list. The index value is an optional parameter for pop() function. If there is no index value, python will throw index error: pop index out of range exception. It takes the index value of an element as an argument. Python pop() function is used to return the removed element from the given list. Then after removing the list element, we printed the updated list. Then we used the remove() function to remove ‘mouse’ from the list. In the above example, we first defined a list called ‘petlist’. Output Updated list of pets: ) Explanation Syntax: List_name.remove(element)Įxample: remove() petlist = If the element doesn’t exist in the list, python throws valueError exception. The remove function takes an element as an argument and removes it from a defined list. ![]() These methods are used for either deleting or removing items from a list. In this article, we will learn about the differences between delete, remove and pop methods in python.Īll these methods are built-in methods available in python. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |