Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python deque

from collections import deque

arr = deque([1, 2])

# append left
arr.appendleft(1)

# append right
arr.append(3)

# count number of 1's in deque
print(arr.count(1))
# output 2

# added 3 new number to the left of deque
arr.extendleft([-1, -2, -3])
print(arr)
# output deque([-3, -2, -1, 1, 1, 2, 3])

# added 3 new number to the right of deque
arr.extend([4, 5, 6])
print(arr)
# output deque([-3, -2, -1, 1, 1, 2, 3, 4, 5, 6])

# insert 10 in the 3rd position
arr.insert(2, 10)
print(arr)
# output deque([-3, -2, 10, -1, 1, 1, 2, 3, 4, 5, 6])

# remove and return far right element
print(arr.pop())
# output 6

# remove and return far left element
print(arr.popleft())
# output -3

# reverse deque
arr.reverse()
print(arr)
# output deque([5, 4, 3, 2, 1, 1, -1, 10, -2])
Source by codefreelance.net #
 
PREVIOUS NEXT
Tagged: #python #deque
ADD COMMENT
Topic
Name
5+9 =