![]() ![]() Print ("The return value is:", cars.pop(-2)) Example: Using the Python List Pop() Function With Negative Indicesįor this example, you will pass negative index values and see what happens to the car list. Print ("The return value is:", cars.pop())Īs you can see in the above output, when you don’t pass an index value, the list pop() function by default removes the last item in the list. But this time, you won’t pass the index argument. In this code, you will use the same car list. Example: Using Pop In Python Without Passing Index This article contains more examples to help you better understand the Python list pop() function. So when you pass 2 as the argument, it will remove the third item. Note: The pop() function removes the third item from the list as the indexing starts with 0. Print ("The return value is:", cars.pop(2)) # Using pop() and printing the return value You can also print the return value by merely printing the pop code, instead of storing it in a separate variable, like this: # Using pop() and storing the return valueĪs you can see in the output, pop in Python returns the popped item. You will then store the returned value in a variable and print it to see what the pop() function returns.Ĭars = In this code, you will create a list of fruits and then use the Python list pop() function to remove an item at a specified index. ![]() ![]() Example: Use Pop In Python to Remove an Item and Return It It is simpler to understand this with an example. The list pop in Python returns the popped value, or in other words, the item present at the specified index value. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |