Complete the code to show the basic operation of a queue in a message broker.
queue = [] queue.append('message1') # Add message to the queue next_message = queue[1]() # Remove message from the queue
In a queue, messages are processed in the order they arrive (FIFO). Using pop(0) removes the first message added.
Complete the code to check if the queue is empty before processing messages.
if len(queue) [1] 0: print('Queue is empty')
Checking if the length of the queue equals zero tells us if it is empty.
Fix the error in the code that tries to add a message to the queue.
queue = [] queue[1]('new_message')
The correct method to add an item to the end of a list (queue) in Python is append.
Fill both blanks to create a dictionary comprehension that maps messages to their lengths, but only for messages longer than 5 characters.
{message: len(message) for message in messages if len(message) [1] 5}The condition len(message) > 5 filters messages longer than 5 characters.
Fill both blanks to create a dictionary comprehension that maps uppercase messages to their lengths, but only for messages containing the letter 'a'.
{message[1]: len(message), for message in messages if 'a' [2] message}Use .upper() to convert messages to uppercase keys, a comma to separate key and value, and in to check if 'a' is in the message.