YouTip LogoYouTip

Python3 Att List Pop

# Python3.x Python3 List pop() Method [![Image 3: Python3 Lists](#) Python3 Lists](#) * * * ## Description The pop() function is used to remove an element from a list (by default, the last element) and return the value of that element. ## Syntax The syntax for the pop() method is: list.pop() ## Parameters * index -- An optional parameter, the index value of the list element to be removed. It cannot exceed the total length of the list. The default is index=-1, which deletes the last list value. ## Return Value This method returns the object of the element removed from the list. ## Example The following example demonstrates the usage of the pop() function: ## Example #!/usr/bin/python3 list1 =['Google','Tutorial','Taobao'] list1.pop() print("List now is : ", list1) list1.pop(1) print("List now is : ", list1) The output of the above example is as follows: List now is : ['Google', 'Tutorial']List now is : ['Google'] A queue is a First-In-First-Out (FIFO) data structure. We can use a list to implement the basic functionality of a queue. * The `append()` method adds an element to the end of the queue. * The `pop()` method removes an element from the beginning of the queue and returns it. ## Example queue =[] # Add elements to the end of the queue queue.append('A') queue.append('B') queue.append('C') # Remove elements from the beginning of the queue and return them print(queue.pop(0))# A print(queue.pop(0))# B print(queue.pop(0))# C In the example above, we created an empty list as a queue and then used the append() method to add three elements to the end of the queue. Next, we used the pop() method to remove elements from the beginning of the queue and return them. Since a queue is a First-In-First-Out data structure, the output we get is 'A', 'B', and 'C'. [![Image 4: Python3 Lists](#) Python3 Lists](#)
← Python3 Att List PopPython3 Att List Index β†’