Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,919 questions

51,852 answers

573 users

How to delete the middle element from a deque in Python

1 Answer

0 votes
from collections import deque

def delete_middle(dq):
    n = len(dq)
    if n == 0:
        return

    mid = n // 2   # 0-based index of middle
    temp = deque()

    # Move first half into temp
    for _ in range(mid):
        temp.append(dq.popleft())

    # Remove the middle element
    dq.popleft()

    # Restore the elements
    while temp:
        dq.appendleft(temp.pop())


from collections import deque

dq = deque([1, 2, 3, 4, 5, 6, 7])
delete_middle(dq)

print(dq)   




'''
run:

deque([1, 2, 3, 5, 6, 7])

'''

 



answered Dec 24, 2025 by avibootz
...