1

I have a queue of nodes that I need to have an upper and lower bound to them, so I have a named tuple called QueueEntry.

QueueEntry = collections.namedtuple('QueueEntry', ('node', 'lower', 'upper'))

When I instantiate a deque with the named tuple inside a list:

bfs_queue = collections.deque([QueueEntry(node, float('-inf'), float('inf'))])

and not:

bfs_queue = collections.deque(QueueEntry(node, float('-inf'), float('inf')))
user22261
  • 47
  • 7
  • 5
    Because `deque` takes an iterable (you can pass in a tuple, list, etc) https://docs.python.org/2/library/collections.html#collections.deque – jspcal Jun 11 '18 at 08:45
  • 1
    Why would you want to use the last version? A deque is a sequence of objects, not a single object. – Daniel Roseman Jun 11 '18 at 08:46

1 Answers1

1

The input to a deque is an iterable, so any iterable data structure will suffice: list, tuple, set, dict, etc.

Alternatively, you can attach an object directly using the append() or appendleft() method:

QueueEntry = collections.namedtuple('QueueEntry', ('node', 'lower', 'upper'))
bfs_queue = collections.deque()
bfs_queue.append(QueueEntry(node, float('-inf'), float('inf')))
bfs_queue.append(QueueEntry(another_node, 0, 0))

Hope this helps :-)

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485