Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

215
Vistas
How to peek front of deque without popping?

I want to check a condition against the front of a queue before deciding whether or not to pop. How can I achieve this in python with collections.deque?

list(my_deque)[0]

seems ugly and poor for performance.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

TL;DR: assuming your deque is called d, just inspect d[0], since the "leftmost" element in a deque is the front (you might want to test before the length of the deque to make sure it's not empty). Taking @asongtoruin's suggestion, use if d: to test whether the deque is empty (it's equivalent to if len(d) == 0:, but more pythonic)

###Why not converting to list? Because deques are indexable and you're testing the front. While a deque has an interface similar to a list, the implementation is optimized for front- and back- operations. Quoting the documentation:

Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

Though list objects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation.

Converting to list might be desirable if you have lots of operations accessing the "middle" of the queue. Again quoting the documentation:

Indexed access is O(1) at both ends but slows to O(n) in the middle. For fast random access, use lists instead.

Conversion to list is O(n), but every subsequent access is O(1).

over 4 years ago · Santiago Trujillo Denunciar

0

You can simply find the last element using my_deque[-1] or my_deque[len(my_deque)-1] .

over 4 years ago · Santiago Trujillo Denunciar

0

Here is a simple implementation that allowed me to check the front of the queue before popping (using while and q[0]):

Apply your own condition against q[0], before q.popleft(), below:

testLst = [100,200,-100,400,340]
q=deque(testLst)

while q:
    print(q)
    print('{}{}'.format("length of queue: ", len(q)))
    print('{}{}'.format("head: ", q[0]))
    print()

    q.popleft()

output:

deque([100, 200, -100, 400, 340])
length of queue: 5
head: 100

deque([200, -100, 400, 340])
length of queue: 4
head: 200

deque([-100, 400, 340])
length of queue: 3
head: -100

deque([400, 340])
length of queue: 2
head: 400

deque([340])
length of queue: 1
head: 340
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda