# How to change the priority of an item in a DataStructures.PriorityQueue?

**URL:** <https://discourse.julialang.org/t/how-to-change-the-priority-of-an-item-in-a-datastructures-priorityqueue/5138>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [July 30, 2017, 3:30pm UTC](https://discourse.julialang.org/t/how-to-change-the-priority-of-an-item-in-a-datastructures-priorityqueue/5138 "2017-07-30T15:30:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 30, 2017, 3:30pm UTC](https://discourse.julialang.org/t/how-to-change-the-priority-of-an-item-in-a-datastructures-priorityqueue/5138/1 "2017-07-30T15:30:48Z")

</div>

Can I remove an item from a DataStructures.PriorityQueue, other than the lowest priority?  
Alternatively, can I change the priority of an item inside a PriorityQueue?

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [July 30, 2017, 11:56pm UTC](https://discourse.julialang.org/t/how-to-change-the-priority-of-an-item-in-a-datastructures-priorityqueue/5138/2 "2017-07-30T23:56:38Z")

</div>

I’ll just point out that you can do both of these operations plus others using SortedDict or SortedMultiDict (use the latter if keys can have multiplicity\>1), which are also in the DataStructures package.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 31, 2017, 12:28am UTC](https://discourse.julialang.org/t/how-to-change-the-priority-of-an-item-in-a-datastructures-priorityqueue/5138/3 "2017-07-31T00:28:44Z")

</div>

Thanks. But I would like to consider the simplest structure that meets my requirements. Are you saying that PriorityQueue does not support modification of priorities?

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [July 31, 2017, 1:20am UTC](https://discourse.julialang.org/t/how-to-change-the-priority-of-an-item-in-a-datastructures-priorityqueue/5138/4 "2017-07-31T01:20:03Z")

</div>

I believe you can do the operations you want with PriorityQueue, but I haven’t tested it. To remove an arbitrary item, use `dequeue_pair!(pq, k)`, where `pq` is the queue and `k` is the key. To change a priority, use `pq[k]=v`, where `v `is the new priority and `k` is the key whose priority you wish to change.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [July 31, 2017, 3:40am UTC](https://discourse.julialang.org/t/how-to-change-the-priority-of-an-item-in-a-datastructures-priorityqueue/5138/5 "2017-07-31T03:40:54Z")

</div>

You can change the weights in a PQ as follows:

```julia
julia> P = PriorityQueue(Char, Int)
DataStructures.PriorityQueue{Char,Int64,Base.Order.ForwardOrdering} with 0 entries

julia> enqueue!(P, 'a', 1)
DataStructures.PriorityQueue{Char,Int64,Base.Order.ForwardOrdering} with 1 entry:
  'a' => 1

julia> enqueue!(P, 'b', 2)
DataStructures.PriorityQueue{Char,Int64,Base.Order.ForwardOrdering} with 2 entries:
  'b' => 2
  'a' => 1

julia> enqueue!(P, 'c', 3)
DataStructures.PriorityQueue{Char,Int64,Base.Order.ForwardOrdering} with 3 entries:
  'b' => 2
  'a' => 1
  'c' => 3

julia> P['a'] = 4
4

julia> P
DataStructures.PriorityQueue{Char,Int64,Base.Order.ForwardOrdering} with 3 entries:
  'b' => 2
  'a' => 4
  'c' => 3

julia> dequeue!(P)
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)

julia> dequeue!(P)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)

julia> dequeue!(P)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

```

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 31, 2017, 8:01am UTC](https://discourse.julialang.org/t/how-to-change-the-priority-of-an-item-in-a-datastructures-priorityqueue/5138/6 "2017-07-31T08:01:48Z")

</div>

Yes, your suggestions work, I just tested them. Thanks.
