Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

queue data structure in python

"""
Queue is a data structure that stores elements in FIFO behaviour.
(FIFO = the item that is least recently added is removed the first)
You can imagine it as an actual queue where the person who ordered first is
first in queue, thus, gets their order served first.

There are various ways to do this, like using a library, making your own, or
just simply using a list
"""
from queue import Queue  # watch out with the capital letters

# Making the queue
queuename = Queue()

# We use .put() to insert value and .get() to pop the first one.
# You can think this as a normal queue

queuename.put(1)  # adds int 1 to index 0
# To access the queue, you need to change it to list
print(list(queuename.queue))
# Output : [1]

queuename.put(2)  # adds int 2 to index 1
print(list(queuename.queue))
# Output: [1,2]

queuename.get()  # removes index 0 (int 1)
print(list(queuename.queue))
# Output: [2]

# We can simulate the same effects using normal list, but the longer the queue
# the more ineffecient it becomes

queuesimulate.append(1)
print(queuesimulate)
# Output : [1]

queuesimulate.append(2)
print(queuesimulate)
# Output: [1,2]

queuesimulate.pop(0)  # 0 is the index number
print(queuesimulate)
# Output: [2]
Comment

PREVIOUS NEXT
Code Example
Python :: python f strings formatting numbers 
Python :: binning continuous values in pyspark 
Python :: forward fill in pyspark 
Python :: python regex compile 
Python :: cv2 jupyter notebook matplotlib inverted colors fix 
Python :: child urls python 
Python :: python gambling machine 
Python :: python datediff days 
Python :: create layer file arcpy 
Python :: Passive to active Python 
Python :: specifying random columns in numpy 
Python :: raspian image with preinstalled python3 
Python :: run python script from applescript 
Python :: docker python no module named 
Python :: ansible custom module 
Python :: negate all elements of a list 
Python :: are you dumb python program 
Python :: loc condition on first 3 columns of dataframe 
Python :: How to assign a value to a dictionary if I need to reference it in the right hand side? 
Python :: python tkinter button multiple commands 
Python :: place a number randomly in a list python 
Python :: python or in if statement 
Python :: What is the purpose of open ( ) and close ( ) in os 
Python :: apply numba to itertools import product 
Python :: list loop get previous element 
Python :: how to mine bitcoin in python 
Python :: create a variable python 
Python :: détruire une variable python 
Python :: Gets an existing SparkSession or, if there is no existing one, creates a new one based on the options set in this builder 
Python :: tkinter e.delete(0,END) 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =