Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Collections module: deques and queues

# Import collections module:
import collections

# Initialize deque:
dq = collections.deque([4, 5, 6])

# Append to the right:
dq.append(7)
print("Append 7 to the right: ", list(dq))

# Append to the left:
dq.appendleft(3)
print("Append 3 to the left: ", list(dq))

# Append multiple values to right:
dq.extend([8, 9, 10])
print("Append 8, 9 and 10 to the right: ", list(dq))

# Append multiple values to left:
dq.extendleft([1, 2])
print("Append 2 and 1 to the left: ", list(dq))

# Insert -1 at index 5
dq.insert(5, -1)
print("Insert -1 at index 5: ", list(dq))

# Pop element from the right end:
dq.pop()
print("Remove element from the right: ", list(dq))

# Pop element from the left end:
dq.popleft()
print("Remove element from the left: ", list(dq))

# Remove -1:
dq.remove(-1)
print("Remove -1: ", list(dq))

# Count the number of times 5 occurs:
i = dq.count(5)
print("Count the number of times 5 occurs: ", i)

# Return index of '7' if found between index 4 and 6:
i = dq.index(7, 4, 6)
print("Search index of number 7 between index 4 and 6: ", i)

# Rotate the deque three times to the right:
dq.rotate(3)
print("Rotate the deque 3 times to the right: ", list(dq))

# Reverse the whole deque:
dq.reverse()
print("Reverse the deque: ", list(dq))
Comment

PREVIOUS NEXT
Code Example
Python :: Python check if caps lock is pressed 
Python :: Display complete information about the DataFrame 
Python :: axios post to django rest return fobidden 403 
Python :: Function to stop a while loop 
Python :: How to check if variable exists in a column 
Python :: Python Tkinter PanedWindow Widget Syntax 
Python :: numerical columns 
Python :: intersection_update() Function of sets in python 
Python :: Shallow copy in python and adding another array to list 
Python :: choose what items on python 
Python :: tables in django 
Python :: create a number of variables based on input in python 
Python :: pandas get most occurring value for each id 
Python :: design patterns in python free download 
Python :: Function argument unpacking in python 
Python :: ValueError: minvalue must be less than or equal to maxvalue 
Python :: python script to open google chrome 
Python :: django muti user for 3 users 
Python :: commend in .env 
Python :: python long multiline text 
Python :: pyqt5 different resolutions 
Python :: travis deployment script for django applications to heroku 
Python :: python dash bootstrap buttons with icons 
Python :: how to loop through glob.iglob iterator 
Python :: print("Default max_rows: {} and min_rows: {}".format( pd.get_option("max_rows"), pd.get_option("min_rows"))) 
Python :: odoo wizard current user login 
Python :: can data scientists become software developer 
Python :: online python text editor 
Python :: python einops rearrange 
Python :: Python match.span() 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =