1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
try:
from collections import deque
except ImportError:
print("SKIP")
raise SystemExit
# Initial sequence is supported
d = deque([1, 2, 3], 10)
# iteration over sequence
for x in d:
print(x)
# Iterables larger than maxlen have the beginnings removed, also tests
# iteration through conversion to list
d = deque([1, 2, 3, 4, 5], 3)
print(list(d))
# Empty iterables are also supported
deque([], 10)
# Extending deques with iterables
d.extend([6, 7])
print(list(d))
# Accessing queue elements via index
d = deque((0, 1, 2, 3), 5)
print(d[0], d[1], d[-1])
# Writing queue elements via index
d[3] = 5
print(d[3])
# Access the last element via index, when the last element is at various locations
d = deque((), 2)
for i in range(4):
d.append(i)
print(i, d[-1])
# Write the last element then access all elements from the end
d[-1] = 4
print(d[-2], d[-1])
# Accessing indices out of bounds raises IndexError
try:
d[4]
except IndexError:
print("IndexError")
try:
d[4] = 0
except IndexError:
print("IndexError")
|