0

So i have this dict:

role_positions = {role1: 1, role2: 2, role3: 3}

How do i reverse the values of it? like this:

role_positions = {role1: 3, role2: 2, role3: 1}
FrostX
  • 23
  • 3
  • 2
    Why do you want to do this? Have you tried anything? – juanpa.arrivillaga Jun 19 '22 at 06:05
  • 2
    Note that dicts don’t necessarily have an order, there are a lot of asterisks to the notion of order in a dict. Hence you may just end up *shuffling* the values, not necessarily reversing them. – deceze Jun 19 '22 at 06:11

1 Answers1

11

You can get values as a list then reversed them and zip with keys and return dict like below:

# python_version > 3.8
>>> dict(zip(role_positions, reversed(role_positions.values())))
{'role1': 3, 'role2': 2, 'role3': 1}

# python_version < 3.8
>>> dict(zip(role_positions, reversed(list(role_positions.values()))))

For older versions of python, for sure about the order of different run-time we need to get the inputted dict as OrderedDict: (here we can read a good explanation)

We suppose we get OrderDict as input, below code is only used for creating OrderDict. if we use the below code in the older version maybe we get a different OrderedDict (because role_positions maybe get a different order) but we suppose the user input OrderDict

from collections import OrderedDict
# we supoose we input below dict
o_dct = OrderedDict((k, v) for k,v in role_positions.items())
dict(zip(o_dct, reversed(list(o_dct.values()))))
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • 3
    `reversed(...)` will work also without `list()` – VPfB Jun 19 '22 at 06:10
  • @VPfB, do you check the above code, without a `list`? – I'mahdi Jun 19 '22 at 06:11
  • 2
    @I'mahdi it will work without `list` (in Python 3.8+) , and also without `.keys()` – juanpa.arrivillaga Jun 19 '22 at 06:12
  • @juanpa.arrivillaga, I got this error: `TypeError: 'dict_values' object is not reversible` – I'mahdi Jun 19 '22 at 06:13
  • 1
    @I'mahdi because you are on a version lower than Python 3.8 – juanpa.arrivillaga Jun 19 '22 at 06:13
  • 1
    @juanpa.arrivillaga, exactly, – I'mahdi Jun 19 '22 at 06:14
  • 1
    I made a mistake with the `reversed` comment, `list()` is required for the old but still supported Python 3.7 and I did not know that. Thanks for correcting me. – VPfB Jun 19 '22 at 06:21
  • 1
    For versions <3.7 the order of insertion is not preserved and the result will be inconsistent **between runs**. – buran Jun 19 '22 at 06:22
  • 1
    @I'mahdi, if you run the script multiple times you will get different result, i,e. different key:value pairs. This will be more evident with dict with more elements – buran Jun 19 '22 at 06:24
  • @buran, Yes you are right if the order of elements in the first dict changed we may have different result – I'mahdi Jun 19 '22 at 06:26
  • @I'mahdi, no, in versions < 3.7 the order of insertion is not preserved. i.e. in different runs of the script, the order of elements in the dict may be (and most likely will be) different and the resulting key:value pairs will be different. Test for your self with e.g. version 3.5 and a dict with e.g. 5 key:value pairs, run it several times – buran Jun 19 '22 at 06:28
  • @buran, yes you are right, I know for previous version and sure about order we need to get `collections. OrderedDict` – I'mahdi Jun 19 '22 at 06:30
  • @buran, how about editing? – I'mahdi Jun 19 '22 at 06:34
  • @I'mahdi, in versions <3.7 the `collections.OrderedDict` has to be used when creating `role_positions`, i.e. it has to be created as `OrderedDict` in order to preserve the order of insertion. What you do will not solve the problem with versions <3.7 because the order in the `role_positions` is not guaranteed and you iterate over dict with "unknown" order. – buran Jun 19 '22 at 06:38
  • @buran, I write exactly, we suppose we get `order_dict` as input if we have older version, I Know creating order_dict generate different order_dict – I'mahdi Jun 19 '22 at 06:39
  • Your `o_dict` is created by iterating over `role_positions`. If `role_positions` is created as regular dict this does not solve the problem in <3.7 – buran Jun 19 '22 at 06:42
  • @buran, I Know in different run_time we get different order_dict – I'mahdi Jun 19 '22 at 06:42
  • @buran, we can not solve in older version only if user input `orderdict`, if use input dict code generate a different result – I'mahdi Jun 19 '22 at 06:47
  • @buran, Do you have a solution for the older version? if the user inputs `dict` and we are sure about the order that we want. – I'mahdi Jun 19 '22 at 06:48
  • @I'mahdi, the only rule they provide for producing the result is to reverse the order. In <3.7 with regular dict (i.e. unclear/inconsistent order of input) it is impossible to produce consistent result without further info/rules. This was noted in deceze comment like 50 minutes ago. – buran Jun 19 '22 at 07:02
  • @buran, exactly, I say the same things but if the user inputs `OrderDict` do you believe we can get same order in each run_time and each python_version? – I'mahdi Jun 19 '22 at 07:04
  • 1
    I already agreed to that - if they create role_positions as ordered dict, they will get consistent result (assuming order of insertion in the OrderedDict is the same, i.e. if there is not some random factor outside their control). – buran Jun 19 '22 at 07:06
  • @buran, exactly ;) – I'mahdi Jun 19 '22 at 07:07