The 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.
Some Operations on Deque
There are various Operations that can be used in Deque. Some of them are listed below with their descriptions:
S. No. | Operation | Description |
---|---|---|
1 | append() | The append() function is utilized for the addition of the data element in its parameter to the right end of the deque. |
2 | appendleft() | The appendleft() function is utilized for the addition of the data element in its parameter to the left end of the deque. |
3 | pop() | The pop() function is utilized for the deletion of the data element from the right end of the deque. |
4 | popleft() | The popleft() function is utilized for the deletion of the data element from the right end of the deque. |
5 | index(element, begin, end) | The index() function is utilized to return the first index value specified in the parameters, start the search from begin till end index. |
6 | insert(i, x) | The insert() function is utilized to insert the value described in the parameter 'x' at index number 'i' mentioned in parameters. |
7 | remove() | The remove() function is utilized to remove the first occurrence of the value specified in the parameters. |
8 | count() | The count() function is utilized to count the total number of occurrences of the value specified in the parameters. |
9 | extend(iterable) | The extend() function is utilized to insert multiple data elements at the right end of the deque. The parameter passed is iterable. |
10 | extendleft(iterable) | The extendleft() function is utilized to insert multiple data elements at the left end of the deque. The parameter passed is iterable. The Order is also reversed as an output of left appends. |
11 | reverse() | The reverse() function is utilized to reverse the Order of deque data elements. |
12 | rotate() | The rotate() function is utilized to rotate the deque by the number mentioned in parameters. If the mentioned number is a negative value, then the rotation occurs to the left. Else rotation is to the right. |
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])
Source : https://www.javatpoint.com/deque-in-python
No comments:
Post a Comment