0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

collections.deque

Posted at
import collections
import queue

collections.deque

q = queue.Queue()
lq = queue.LifoQueue()
l = []
d = collections.deque()

for i in range(3):
    q.put(i)
    lq.put(i)
    l.append(i)
    d.append(i)

for _ in range(3):
    print('FIFO queue = {}'.format(q.get()))
    print('LIFO queue = {}'.format(lq.get()))
    print('list       = {}'.format(l.pop())) #pop(0)でFIFO
    print('deque      = {}'.format(d.pop())) #d.popleft()でFIFO
    print()

実行結果:

FIFO queue = 0
LIFO queue = 2
list       = 2
deque      = 2

FIFO queue = 1
LIFO queue = 1
list       = 1
deque      = 1

FIFO queue = 2
LIFO queue = 0
list       = 0
deque      = 0
import collections
import queue

collections.deque

d = collections.deque()

for i in range(3):
    d.append(i)

print(d)
d.extendleft('x')
d.extend('y')
print(d)
d.rotate()
print(d)
d.clear()
print(d)

実行結果:

deque([0, 1, 2])
deque(['x', 0, 1, 2, 'y'])
deque(['y', 'x', 0, 1, 2])
deque([])
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?