Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I need to use a priority queue in my Python code. Looking around for something efficient, I came upon heapq. It looks good but seems to be specified only for integers. I suppose it works with any objects that have comparison operators, but it doesn't specify what comparison operators it needs.

Besides, heapq seems to be implemented in Python, so it's not fast.

Are you aware of any fast implementations for priority queues in Python? Optimally, I'd like the queue to be generic (i.e. work well for any object with a specified comparison operator).

Thanks in advance

1 Answer

0 votes
by (106k points)

To get a generic priority queue for Python you can use Queue.PriorityQueue.

Python is not strongly typed, so you can save anything you like: just make a tuple of (priority, thing) and you're set.

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
asked Feb 18, 2020 in Java by angadmishra (6.5k points)
0 votes
1 answer
asked Dec 27, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
0 votes
1 answer
asked Feb 19, 2021 in Java by sheela_singh (9.5k points)

Browse Categories

...