Python

How to peek at front of deque in Python

A deque is mainly a simplification of the stack and queue arrangement, which is adjusted from the left side to the right side. Offers O(1) time complication for popping and additions. Deques are the usual library class that exists in collection modules. Must be imported into a standard collection library module before being used first. In this article, we are discussing different functions of the deque class.

Popping functions:

We remove or delete the elements from the deque by applying the pop() function. This function has two different kinds. The pop() approach is utilized to delete the rightmost item within the queue and return it.

In this example, first, we import collections. For this, we utilize a variable col. After this, we take a variable named my_deque. The queue we take here is ‘0683uv’. We apply the print statement to print this dequeue. Now we remove elements from left and right positions. In the end, we apply a print statement to get the de queue after applying these pop operations.

Now run the code by clicking the “Run” button from the menu bar. The print statement prints the dequeue. The elements in this dequeue are separated by commas. We separately display the elements which are removed from the right and left of the queue. After that print statement prints the dequeue after removing the given elements from left and right.

Item related functions:

We use some dequeue functions to acquire data related to elements. Here we utilize the function index() to obtain the position of the first existence. If no parameter is passed on the item, the complete list is selected, and if an exact limit is stated, the index within this limit is checked. In this case, we take a queue ‘ijjjjklmnopnq.’ The variable used for this queue is ‘my_deque.’ In this queue, we repeated different elements. J is repeated 4 times, n is repeated 2 times. Now we want to know about an index of j in the whole string, and in-between the index of string 4 to 10, so we apply a print statement for this. After this, we get to know the occurrences of repeated elements n and j.

We get a queue after applying the print statement, and items are detached by commas. We get the index of j in the string, which is 1, and the index of j between the ranges 4 to 10, which is 4. In the end, we get the occurrences of n=2 and j=4 separately.

Insert () and Remove () functions:

We have previously covered the pop function for removing elements in the deque. Here are two other ways to insert and delete the elements. The inser () approach is utilized to insert an element. In this situation, we may arrange an index for insertion. In this way, you can insert an element at a specific point. Also, the remove() technique is utilized to delete the first existence of any item.

In this instance, the queue on which we apply different operations is ‘stuuwxxyz.’ It is a mixture of repeated and single alphabets. Variable my_deque is used for this queue. First, we print the whole queue. After this, we apply a function to insert the element ‘o’ at the position ‘3’ in the queue. Further, we apply the function to insert the item ‘r’ at the position of ‘6’ in the original queue. In the end, we apply the remove operation to remove the element ‘x’ from the definite queue.

In the output, the print statement first prints the original dequeue then prints the dequeue after inserting the elements o and r. After this, prints the dequeue after removing element x.

Extending functions:

Extension functions are utilized to insert multiple items to a deque. We may provide numerous values ​​using collections such as lists and tuples. We have two sorts of extension functions. The extend() process is utilized to insert an item on the right side of the string. This function is related to repeating an append () approach. Also, utilize the extendleft () technique to insert the items to the left. This method is similar to the iterative appendleft () method.

Here we take different alphabets ‘fghhijklllm’ in the queue. We print the queue after calling the print statement. We want to extend the queue by adding some elements at the start and end of the queue. So for this insertion, we apply to extend functionality in this program.

First, we get the string dequeue. Then we get the dequeue after the addition of elements ‘pqrst’ to the right of the string and ‘948’ to the left side of the string. So by this, in the end, the dequeue becomes ‘849fghhijklllmpqrst’.

Conclusion:

In this article, we have deliberated some functions of deque with examples. The functions pop() and popleft() are utilized to delete the elements from the left and right sides of the queue. Append and appendleft() functions are utilized to append the items in the queue. Some dequeue functions are used to obtain data associated with elements. We are using the function inde () to obtain the position of the first existence element. Other than pop() and append(), there are two more functions for inserting and removing elements from the queue. We also prolong the queue by applying extending functions.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content

Page was generated in 0.05220890045166