self items append(itemdef add_rear(selfitem)self items insert( ,itemdef remove_front(self)return self items pop(def remove_rear(self)return self items pop( def size(self)return len(self itemsin remove_front we use the pop method to remove the last element from the list howeverin remove_rearthe pop( method must remove the first element of the list likewisewe need to use the insert method in add_rear since the append method assumes the addition of new element to the end of the list you can see many similarities to python code already described for stacks and queues you are also likely to observe that in this implementation adding and removing items from the front is ( whereas adding and removing from the rear is (nthis is to be expected given the common operations that appear for adding and removing items againthe important thing is to be certain that we know where the front and rear are assigned in the implementation palindrome checker an interesting problem that can be easily solved using the deque data structure is the classic palindrome problem palindrome is string that reads the same forward and backwardfor exampleradartootand madam we would like to construct an algorithm to input string of characters and check whether it is palindrome the solution to this problem will use deque to store the characters of the string we will process the string from left to right and add each character to the rear of the deque at this pointthe deque will be acting very much like an ordinary queue howeverwe can now make use of the dual functionality of the deque the front of the deque will hold the first character of the string and the rear of the deque will hold the last character (see figure since we can remove both of them directlywe can compare them and continue only if they match if we can keep matching first and the last itemswe will eventually either run out of characters or be left with deque of size depending on whether the length of the original string was even or odd in either casethe string must be palindrome import deque as previously defined def pal_checker(a_string)char_deque deque(for ch in a_string basic data structures