Deque in PythonThe Queue is a core library that allows the users to define a list based on the FIFO (First In, First Out) principle. In contrast, the Deque in Python owns the opposite principle: LIFO (Last in, First Out) queue. In the following tutorial, we will only understand what Deque in Python is with some examples. So, let's get started. Understanding the Deque in PythonA Deque, also known as a Double-ended queue, has the attribute of inserting and deleting data elements from either end. The deque module is a segment of the library known as collections. It contains the attributes to add and remove data elements that can be invoked directly with parameters. In order to declare a deque, we have to import the collections library first. Let us consider the following syntax to understand how the deque module works in Python. Syntax: Explanation: In the above snippet of code, we have imported the deque module from the collections library and declared the deque by assigning the name of the list, i.e., list_name in the above case, to the deque() module. Here we can also observe that we do not need any class in order to implement these in-built methods. They can be implemented directly. Let us consider a simple example based on the deque module. Example: Output: deque(['Apple', 'Mango', 'Peaches', 'Banana', 'Papaya']) Explanation: In the above example, we have imported the deque module from the collections library. We have then defined a fruit list as a deque using the deque module, specifying some of the fruit names. We have then printed the declared Deque for the users. As a result, the declared Deque containing a bunch of fruit names is printed successfully. Now, let us understand various Operations on Deque. Some Operations on DequeThere are various Operations that can be used in Deque. Some of them are listed below with their descriptions:
Now let us consider some examples based on the deque module. Example: Output: The deque after appending at right: deque([10, 20, 30, 40, 50, 60]) The deque after appending at left: deque([70, 10, 20, 30, 40, 50, 60]) The deque after removing from right: deque([70, 10, 20, 30, 40, 50]) The deque after removing from left: deque([10, 20, 30, 40, 50]) Explanation: In the above snippet of code, we have imported the collections library and declared a deque. We have then used the operations like append() and appendleft() in order to insert the some data elements to both ends of the deque and printed the modified deque for the users. Similarly, we have then used the operations such as pop() and popleft() in order to remove the data elements from both ends of the deque and printed the resultant deque for the users. Example: Output: The first occurs of 'Feb' at a position: 4 The deque after inserting 'Jan' at 4th position: deque(['Jan', 'Feb', 'Mar', 'Jan', 'Mar', 'Feb', 'April', 'Feb']) The count of 'Feb' in deque: 3 The deque after removing the first occurrence of 'Mar': deque(['Jan', 'Feb', 'Jan', 'Mar', 'Feb', 'April', 'Feb']) Explanation: In the above snippet of code, we have again imported the collections library and declared a deque. We have then used the index() operation in order to check the first occurrence of the data element 'Feb' between the index numbers 2 and 7, respectively. We have then used the insert() operation in order to insert the data element 'Jan' at the 4th position. We have then used the count() operation in order to count the occurrence of the data element 'Feb' in the deque. At last, we have used the remove() operation in order to remove the first occurrence of the data element 'Mar' from the deque and printed the resultant deque for the users. Next TopicDictionary Comprehension in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India