Built with Tutify.io

    AIStudyLabs

    Python Deque Methods

    Python Deque Methods

    In Python we have several deque methods, and using these methods we can implement deques very efficiently. The deque stands for Double Ended Queue which is a one-dimensional data structure like a list in python. In this data structure, we can perform insertion and deletion operations from both ends.

    In this article, we will discuss different methods available in a deque data structure in Python and learn how we can append, rotate, reverse, extend, count the items, and remove the items from both sides.

    1. Deque Methods in Python

    Following are the methods of Deque.

    | Deque Methods | Description | | --- | --- | | append(item) | Adds an item to the right | | appendleft(item) | Adds an item to the left | | extend([items]) | Add multiple items to the right | | extendleft([items]) | Add multiple items to the right | | pop() | Removes the last item | | popleft() | Removes the first item | | insert(index_pos,item) | Item is inserted at the specified index position | | len(deque) | Return the total number of elements present in the deque | | deque.reverse() | Reverse the elements in the deque | | rotate(value) | Rotates particular elements in the deque to the left/right | | count(item) | Return the total occurrences of an item | | index(item,start,end) | Return the first occurrence index position of an item. |

    <a id="h-2-python-deque-append-method"></a>

    2. Python Deque append() Method

    The append() method will add only a single item to the deque. It takes an item as a parameter and is inserted at the last position.

    <a id="h-2-1-syntax"></a>

    2.1 Syntax

    Following is the syntax of the append() method

    
    # Here, deque1 is the input deque.
    # Add single item to the right
    deque1.append(item)
    

    <a id="h-2-2-deque-append-example"></a>

    2.2 Deque append() Example

    Let's create a deque with 5 countries and add an item to the right of the deque using append() method.

    
    from collections import deque
    
    # Declare deque with 5 countries
    deque = deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
    print("Deque: ",deque)
    
    # Using append() to append the item
    deque.append('Norway')
    print("Deque: ",deque)
    

    Yields below output.

    We have added 'Norway' to the deque and it is added at the last (right). Now there are 6 countries in the deque.

    <a id="h-3-python-deque-appendleft-method"></a>

    3. Python Deque appendleft() Method

    The appendleft() will add only a single item to the deque. It takes an item as a parameter. Item is inserted at the first position.

    <a id="h-3-1-syntax"></a>

    3.1 Syntax

    Following is the syntax of the appendleft() method

    
    # Here, deque1 is the input deque.
    # Add single item to the right
    deque1.appendleft(item)
    

    <a id="h-3-2-example"></a>

    3.2 Example

    Let's create a deque with 5 countries and add an item to the left of the deque using appendleft() method.

    
    # Using appendleft() to append the item left of deque
    deque.appendleft('Norway')
    print("Deque: ",deque)
    

    Yields below output.

    We have added 'Norway' to the deque and it is added at the first (left). Now there are 6 countries in the deque.

    <a id="h-4-python-deque-extend-method"></a>

    4. Python Deque extend() Method

    extend() adds single or multiple items to the deque . It takes a list of items as a parameter. Items are inserted at the last position one by one.

    <a id="h-4-1-syntax"></a>

    4.1 Syntax

    Following is the syntax of the extend() method

    
    # Here, deque1 is the input deque.
    deque1.extend(item)
    

    <a id="h-4-2-example"></a>

    4.2 Example

    Let's create deque with 5 countries and add 2 items to the right of the deque using the extend() method.

    
    from collections import deque
    
    # Declare deque with 5 countries
    deque = deque(['China','Russia','England','Nepal','Japan'])
    print("Deque: ",deque)
    
    # Using extend() to extend the deque
    deque.extend(['Norway','Italy'])
    print("Deque: ",deque)
    
    # Output:
    # Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
    # Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan', 'Norway', 'Italy'])
    

    'Norway' and 'Italy' were added to the deque one after the other.

    <a id="h-5-python-deque-extendleft-method"></a>

    5. Python Deque extendleft() Method

    extendleft() adds single or multiple items to the deque . It takes list of items as a parameter. Items are inserted at the first position (The last element is added first).

    <a id="h-5-1-syntax"></a>

    5.1 Syntax

    Following is the syntax of the extendleft() method

    
    # Here, deque1 is the input deque.
    deque1.extendleft(item)
    

    <a id="h-5-2-example"></a>

    5.2 Example

    Let's create a deque with 5 countries and add 2 items to the left of the deque using the extendleft() method.

    
    from collections import deque
    
    # Declare deque with 5 countries
    deque = deque(['China','Russia','England','Nepal','Japan'])
    print("Deque: ",deque)
    
    # Using extendleft() to extend the deque from left
    deque.extendleft(['Norway','Italy'])
    print("Deque: ",deque)
    
    # Output:
    # Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
    # Deque:  deque(['Italy', 'Norway', 'China', 'Russia', 'England', 'Nepal', 'Japan'])
    

    'Norway' and 'Italy' were inserted at left in the deque.

    <a id="h-6-python-deque-pop-method"></a>

    6. Python Deque pop() Method

    pop() method removes the last element present in the deque and returns the popped item. It takes no parameter.

    If the deque is empty. It will return an "IndexError - pop from an empty deque". This scenario is handled with try-except block.

    <a id="h-6-1-syntax"></a>

    6.1 Syntax

    Following is the syntax of the pop() method

    
    # Here, deque1 is the input deque.
    deque1.pop()
    

    <a id="h-6-2-example"></a>

    6.2 Example

    Let's create deque with 5 countries and use pop() method to remove last item from the deque.

    
    from collections import deque
    
    # Declare deque with 5 countries
    deque = deque(['China','Russia','England','Nepal','Japan'])
    
    # Using pop() to remove last item
    print("Popped item: ",deque.pop())
    print("Deque: ",deque)
    
    # Output:
    # Popped item:  Japan
    # Deque:  deque(['China', 'Russia', 'England', 'Nepal'])
    

    'Japan' is removed from the deque since it is the last item.

    <a id="h-7-python-deque-popleft-method"></a>

    7. Python Deque popleft() Method

    popleft() removes the first element present in the deque and returns the popped item. It takes no parameter.

    If the deque is empty. It will return an "IndexError - pop from an empty deque". This scenario is handled with try-except block.

    <a id="h-7-1-syntax"></a>

    7.1 Syntax

    Following is the syntax of the popleft() method

    
    # Here, deque1 is the input deque.
    deque1.popleft()
    

    <a id="h-7-2-example"></a>

    7.2 Example

    Let's create a deque with 5 countries and use popleft() method to remove the first item from the deque.

    
    from collections import deque
    
    # Declare deque with 5 countries
    deque = deque(['China','Russia','England','Nepal','Japan'])
    
    # Using popleft() to remove first item
    print("Popped item: ",deque.popleft())
    print("Deque: ",deque)
    
    # Output:
    # Popped item:  China
    # Deque:  deque(['Russia', 'England', 'Nepal', 'Japan'])
    

    'China' is removed from the deque since it is the first item.

    <a id="h-8-python-deque-insert-method"></a>

    8. Python Deque insert() Method

    insert() will add an item at the specified position. By using the index position, an item is inserted.

    It will take index_pos as the first parameter that specifies the index position and the item to be inserted as the second parameter.

    <a id="h-8-1-syntax"></a>

    8.1 Syntax

    Following is the syntax of the insert() method

    
    # Here, deque1 is the input deque.
    deque1.insert(index_pos,item)
    

    <a id="h-8-2-example"></a>

    8.2 Example

    Let's create a deque with 5 countries and insert - 'New city' at 2nd index position.

    
    from collections import deque
    
    # Declare deque with 5 countries
    deque = deque(['China','Russia','England','Nepal','Japan'])
    print("Deque: ",deque)
    
    # Using insert() to insert an item at specified position
    deque.insert(2,'New city')
    print("Deque: ",deque)
    
    # Output:
    # Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
    # Deque:  deque(['China', 'Russia', 'New city', 'England', 'Nepal', 'Japan'])
    

    We can see that 'New city' is inserted at 2nd index position.

    <a id="h-9-python-deque-count-method"></a>

    9. Python Deque count() Method

    count() will return the total number of occurrences if specified item. It will take item as a parameter.

    <a id="h-9-1-syntax"></a>

    9.1 Syntax

    Following is the syntax of the count() method

    
    # Here, deque1 is the input deque.
    deque1.count(item)
    

    <a id="h-9-2-example"></a>

    9.2 Example

    Let's create deque with 5 countries and return the total occurrences of 'England'.

    
    from collections import deque
    
    # Declare deque with 5 countries
    deque = deque(['England','England','England','England','Japan'])
    print("Deque: ",deque)
    
    # Using count() to count the specified item
    print(deque.count('England'))
    
    # Output:
    # Deque:  deque(['England', 'England', 'England', 'England', 'Japan'])
    # 4
    

    'England' occurred 4 times in the deque.

    <a id="h-10-python-deque-len-method"></a>

    10. Python Deque len() Method

    len() will return the total number of elements present in the deque. It takes deque as an input parameter.

    <a id="h-10-1-syntax"></a>

    10.1 Syntax

    Following is the syntax of the len() method

    
    # Here, deque1 is the input deque.
    len(deque1)
    

    <a id="h-10-2-example"></a>

    10.2 Example

    Let's create deque with 5 countries and return the length of it.

    
    from collections import deque
    
    # Declare deque with 5 countries
    deque = deque(['England','Japan','China','England','England'])
    print("Deque: ",deque)
    
    # Using len() to get the number of item
    print(len(deque))
    
    # Output:
    # Deque:  deque(['England', 'Japan', 'China', 'England', 'England'])
    # 5
    

    Total number of elements is 5.

    <a id="h-11-python-deque-reverse-method"></a>

    11. Python Deque reverse() Method

    reverse() is used to reverse the entire deque. It takes no parameter. It won't return anything. If we display the deque after applying the reverse() function, we can see the elements are returned in the reverse order.

    <a id="h-11-1-syntax"></a>

    11.1 Syntax

    Following is the syntax of the reverse() method

    
    # Here, deque1 is the input deque.
    deque1.reverse()
    

    <a id="h-11-2-example"></a>

    11.2 Example

    Let's create deque with 5 countries and reverse it.

    
    from collections import deque
    
    # Declare deque with 5 countries
    deque = deque(['China','Russia','England','Nepal','Japan'])
    print("Deque: ",deque)
    
    # Using reverse() to reverse the deque
    deque.reverse()
    print("Deque: ",deque)
    
    # Output:
    # Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
    # Deque:  deque(['Japan', 'Nepal', 'England', 'Russia', 'China'])
    

    We can see that elements in the deque are reversed.

    <a id="h-12-python-deque-rotate-method"></a>

    12. Python Deque rotate() Method

    rotate() is used to shift the elements within the deque. It takes an integer which specifies the number of elements to be rotated.

    If the integer is positive, elements are rotated to right, Otherwise elements will be rotated to left.

    <a id="h-12-1-syntax"></a>

    12.1 Syntax

    Following is the syntax of the rotate() method

    
    # Here, deque1 is the input deque.
    deque1.rotate(value)
    

    <a id="h-12-2-examples"></a>

    12.2 Examples

    Example 1: Let's create deque with 5 countries and rotates by 2 to right.

    
    from collections import deque
    
    # Declare deque with 5 countries
    deque = deque(['China','Russia','England','Nepal','Japan'])
    print("Deque: ",deque)
    
    # Using rotate() to rotate the deque
    deque.rotate(2)
    print("Deque: ",deque)
    
    # Output:
    # Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
    # Deque:  deque(['Nepal', 'Japan', 'China', 'Russia', 'England'])
    

    Example 2: Let's create deque with 5 countries and rotates by 2 to left.

    
    from collections import deque
    
    # Declare deque with 5 countries
    deque = deque(['China','Russia','England','Nepal','Japan'])
    print("Deque: ",deque)
    
    # rotate()
    deque.rotate(-2)
    print("Deque: ",deque)
    
    # Output:
    # Deque:  deque(['China', 'Russia', 'England', 'Nepal', 'Japan'])
    # Deque:  deque(['England', 'Nepal', 'Japan', 'China', 'Russia'])
    

    <a id="h-13-python-deque-index-method"></a>

    13. Python Deque index() Method

    index() method is used to return the index of particular item specified in it. It there are multiple items in the deque, first occurrence index position is returned.

    <a id="h-13-1-syntax"></a>

    13.1 Syntax

    Following is the syntax of the index() method

    
    # Here, deque1 is the input deque.
    deque1.index(item,start,end)
    

    <a id="h-13-2-parameters"></a>

    13.2 Parameters

    It takes three parameters. Seconsdand third parameter is optional.

    1. item to be searched in the deque
    2. start specifies the start position in which the item is searched from this position.
    3. end specifies the stop/end position in which the item is searched till this position.

    <a id="h-13-3-example"></a>

    13.3 Example

    Let's create deque with 5 countries and use index() method to return indices of particular element.

    
    from collections import deque
    
    # Declare deque with 5 countries
    deque = deque(['England','Japan','China','England','England'])
    print("Deque: ",deque)
    
    # index()
    print(deque.index('England',0,4))
    
    # index()
    print(deque.index('England',2,4))
    
    # Output:
    # Deque:  deque(['England', 'Japan', 'China', 'England', 'England'])
    # 0
    # 3
    
    1. From position -0 to 4: First index position of 'England' is 0.
    2. From position -2 to 4: First index position of 'England' is 3.

    <a id="h-14-conclusion"></a>

    14. Conclusion

    In this article, we have seen all 12 methods supported by the deque data structure in python. And also learned how we can perform the deque from both sides with examples.

    <a id="h-related-articles"></a>

    Related Articles