how to print the element at front of a queue python 3? [duplicate]
up vote
-1
down vote
favorite
This question already has an answer here:
examining items in a python Queue
2 answers
import queue
q = queue.Queue()
q.put(5)
q.put(7)
print(q.get()) removes the element at front of the queue. How do i print this element without removing it? Is it possible to do so?
python python-3.x syntax queue python-collections
marked as duplicate by slider, Community♦ Nov 10 at 10:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
-1
down vote
favorite
This question already has an answer here:
examining items in a python Queue
2 answers
import queue
q = queue.Queue()
q.put(5)
q.put(7)
print(q.get()) removes the element at front of the queue. How do i print this element without removing it? Is it possible to do so?
python python-3.x syntax queue python-collections
marked as duplicate by slider, Community♦ Nov 10 at 10:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
If you're not using your queue as an inter-thread communication mechanism, you should be usingcollections.deque
, notqueue.Queue
. If you are using your queue to communicate between threads, then a peek operation is rarely useful or safe for such use cases, and you should think carefully about whether you need it.
– user2357112
Nov 10 at 0:39
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
This question already has an answer here:
examining items in a python Queue
2 answers
import queue
q = queue.Queue()
q.put(5)
q.put(7)
print(q.get()) removes the element at front of the queue. How do i print this element without removing it? Is it possible to do so?
python python-3.x syntax queue python-collections
This question already has an answer here:
examining items in a python Queue
2 answers
import queue
q = queue.Queue()
q.put(5)
q.put(7)
print(q.get()) removes the element at front of the queue. How do i print this element without removing it? Is it possible to do so?
This question already has an answer here:
examining items in a python Queue
2 answers
python python-3.x syntax queue python-collections
python python-3.x syntax queue python-collections
edited Nov 10 at 0:35
asked Nov 10 at 0:29
Ankit Jain
35
35
marked as duplicate by slider, Community♦ Nov 10 at 10:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by slider, Community♦ Nov 10 at 10:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
If you're not using your queue as an inter-thread communication mechanism, you should be usingcollections.deque
, notqueue.Queue
. If you are using your queue to communicate between threads, then a peek operation is rarely useful or safe for such use cases, and you should think carefully about whether you need it.
– user2357112
Nov 10 at 0:39
add a comment |
If you're not using your queue as an inter-thread communication mechanism, you should be usingcollections.deque
, notqueue.Queue
. If you are using your queue to communicate between threads, then a peek operation is rarely useful or safe for such use cases, and you should think carefully about whether you need it.
– user2357112
Nov 10 at 0:39
If you're not using your queue as an inter-thread communication mechanism, you should be using
collections.deque
, not queue.Queue
. If you are using your queue to communicate between threads, then a peek operation is rarely useful or safe for such use cases, and you should think carefully about whether you need it.– user2357112
Nov 10 at 0:39
If you're not using your queue as an inter-thread communication mechanism, you should be using
collections.deque
, not queue.Queue
. If you are using your queue to communicate between threads, then a peek operation is rarely useful or safe for such use cases, and you should think carefully about whether you need it.– user2357112
Nov 10 at 0:39
add a comment |
1 Answer
1
active
oldest
votes
up vote
-1
down vote
accepted
The Queue object has a collections.deque object attribute. Please see the Python documentation on accessing elements of the deque in regards to efficiency. A list may be a better use-case if you need to access elements randomly.
import queue
if __name__ == "__main__":
q = queue.Queue()
q.put(5)
q.put(7)
"""
dir() is helpful if you don't want to read the documentation
and just want a quick reminder of what attributes are in your object
It shows us there is an attribute named queue in the Queue class
"""
for attr in dir(q):
print(attr)
#Print first element in queue
print("nLooking at the first element")
print(q.queue[0])
print("nGetting the first element")
print(q.get())
print("nLooking again at the first element")
print(q.queue[0])
Note: I have abbreviated the output from the dir iterator
>>>
put
put_nowait
qsize
queue
task_done
unfinished_tasks
Looking at the first element
5
Getting the first element
5
Looking again at the first element
7
>>>
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
accepted
The Queue object has a collections.deque object attribute. Please see the Python documentation on accessing elements of the deque in regards to efficiency. A list may be a better use-case if you need to access elements randomly.
import queue
if __name__ == "__main__":
q = queue.Queue()
q.put(5)
q.put(7)
"""
dir() is helpful if you don't want to read the documentation
and just want a quick reminder of what attributes are in your object
It shows us there is an attribute named queue in the Queue class
"""
for attr in dir(q):
print(attr)
#Print first element in queue
print("nLooking at the first element")
print(q.queue[0])
print("nGetting the first element")
print(q.get())
print("nLooking again at the first element")
print(q.queue[0])
Note: I have abbreviated the output from the dir iterator
>>>
put
put_nowait
qsize
queue
task_done
unfinished_tasks
Looking at the first element
5
Getting the first element
5
Looking again at the first element
7
>>>
add a comment |
up vote
-1
down vote
accepted
The Queue object has a collections.deque object attribute. Please see the Python documentation on accessing elements of the deque in regards to efficiency. A list may be a better use-case if you need to access elements randomly.
import queue
if __name__ == "__main__":
q = queue.Queue()
q.put(5)
q.put(7)
"""
dir() is helpful if you don't want to read the documentation
and just want a quick reminder of what attributes are in your object
It shows us there is an attribute named queue in the Queue class
"""
for attr in dir(q):
print(attr)
#Print first element in queue
print("nLooking at the first element")
print(q.queue[0])
print("nGetting the first element")
print(q.get())
print("nLooking again at the first element")
print(q.queue[0])
Note: I have abbreviated the output from the dir iterator
>>>
put
put_nowait
qsize
queue
task_done
unfinished_tasks
Looking at the first element
5
Getting the first element
5
Looking again at the first element
7
>>>
add a comment |
up vote
-1
down vote
accepted
up vote
-1
down vote
accepted
The Queue object has a collections.deque object attribute. Please see the Python documentation on accessing elements of the deque in regards to efficiency. A list may be a better use-case if you need to access elements randomly.
import queue
if __name__ == "__main__":
q = queue.Queue()
q.put(5)
q.put(7)
"""
dir() is helpful if you don't want to read the documentation
and just want a quick reminder of what attributes are in your object
It shows us there is an attribute named queue in the Queue class
"""
for attr in dir(q):
print(attr)
#Print first element in queue
print("nLooking at the first element")
print(q.queue[0])
print("nGetting the first element")
print(q.get())
print("nLooking again at the first element")
print(q.queue[0])
Note: I have abbreviated the output from the dir iterator
>>>
put
put_nowait
qsize
queue
task_done
unfinished_tasks
Looking at the first element
5
Getting the first element
5
Looking again at the first element
7
>>>
The Queue object has a collections.deque object attribute. Please see the Python documentation on accessing elements of the deque in regards to efficiency. A list may be a better use-case if you need to access elements randomly.
import queue
if __name__ == "__main__":
q = queue.Queue()
q.put(5)
q.put(7)
"""
dir() is helpful if you don't want to read the documentation
and just want a quick reminder of what attributes are in your object
It shows us there is an attribute named queue in the Queue class
"""
for attr in dir(q):
print(attr)
#Print first element in queue
print("nLooking at the first element")
print(q.queue[0])
print("nGetting the first element")
print(q.get())
print("nLooking again at the first element")
print(q.queue[0])
Note: I have abbreviated the output from the dir iterator
>>>
put
put_nowait
qsize
queue
task_done
unfinished_tasks
Looking at the first element
5
Getting the first element
5
Looking again at the first element
7
>>>
answered Nov 10 at 0:59
Kristian
1054
1054
add a comment |
add a comment |
If you're not using your queue as an inter-thread communication mechanism, you should be using
collections.deque
, notqueue.Queue
. If you are using your queue to communicate between threads, then a peek operation is rarely useful or safe for such use cases, and you should think carefully about whether you need it.– user2357112
Nov 10 at 0:39