Ticker

6/recent/ticker-posts

Deque Python | Examples - Datacloudy



 In this blog we are going to see about Deque in Python. In this single page blog, you will get the all necessary concept of deque in crisp and clear manner.

Deque means Doubly Ended Queue. We can use deque in the place of list when we need quick append and pop operation from both the ends of the container.

The time complexity of list for append and pop is O(n)

The time complexity of deque for append and pop is O(1)  

we looked about time complexity of list and deque and understood the efficiency of deque. Now we go for deque topic explanation,

For access the deque, first we need to import it. Now let us see one example.

from collections import deque

created_deque=deque(['a','b','c'])  


Note:  For all the examples given below , the created_deque value is ['a','b','c']


    



Adding Elements to deque:

  • append()  :   
         Add element at last position of the deque.

created_deque.append('d')

result:
    deque(['a','b','c','d'])  

          In the above example we can see that the append() function adds the  element 'd' at the end of the Queue. 


  • appendleft() 
        Add element at first position of the deque.


created_deque.appendleft('e')

result:
    deque(['e','a','b','c'])  

         Inn the above example we can see that the appendleft() function adds the  element 'e' at the beginning of the Queue.

Deleting element from deque:

  • pop()  
         delete an element at last position of the deque.


created_deque.pop( )

result:
    deque(['a','b'])   


  • popleft()  : 
         delete an element at first position of the deque. That is delete from left side.

created_deque.popleft( )

result:
    deque(['b','c'])   


Accessing the element:

  • index(element,start_position,end_position) :
        finds the index position of the element within the range of start and end position.

created_deque.index('a',0,1)

result:
    0  

 

  • insert(index,element)  :
        this will insert the element at the mentioned index.

created_deque.index(1,'d')

result:
    deque(['a','d','b','c'])  

 

  • remove(element)
        delete the first ocurrance  of the element.

created_deque.remove('a')

result:
    deque(['b','c'])  

 

  • count(element) 
        count the number of element in the deque.

created_deque.count('b')

result:
    1  


Different Operations on deque:

  • extend(iterable)
        we can extend the deque on the right side.

created_deque.extend(['d','e'])

result:
    deque(['a','b','c','d','e'])    

        In the above example we have to consider that the deque already present is ['a','b','c'] and now due to extend() keyword we are added ['d','e'] at the right side. 

 

  • extendleft(iterable)  :
        we can also extend the deque from the left. 

created_deque.extendleft(['d','e'])

result:
    deque(['d','e','a','b','c'])  

 

  • reverse() :  we can reverse the deque. 

created_deque.reverse()

result:
    deque(['c','b','a'])  

 

  • rotate(times) : we can rotate the deque in right based on the number of times mentioned. 

created_deque.rotate(1)

result:
    deque(['b','c','a'])  

 

  • rotate(-times)  : we can rotate the  deque in left based on the number of times mentioned. 

created_deque.rotate(-1)

result:
    deque(['c','a'',b',])   


In this blog we had a simple and all required information about deque in python. Hope this information will be helpful for you.

If you like this blog and want to show your support, please click follow button on the right side pane of this window. You can also follow us in social media by clicking on any of the social media plugin given above the follow button.


Thank You!!!


Post a Comment

0 Comments

Ad Code