2

I want to reverse the order of a string i.e. If i have:

dictionaryA = {'A':'a', 'B':'b', 'C':'c', 'D':'d'}

i want to rearrange it to where it's:

{'D':'d','C':'c', 'B':'b', 'A':'a'}

Why won't splicing work? ([::-1])? And reverse() too? How can you rearrange the order of each dictionary entry?

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • 1
    Although dictionaries in current versions of Python do preserve order, why do you need to change the order? In what use case is that important? – Grismar Dec 29 '20 at 01:35
  • 1
    just `dict(reversed(dictionaryA.items())` on the most recent versions of Python, you may have to do `dict(reversed(list(dictionaryA.items()))` – juanpa.arrivillaga Dec 29 '20 at 01:37
  • What did you try? This should work : `reversed = dict(list(dictionaryA.items())[::-1])` – cvanelteren Dec 29 '20 at 01:37
  • 1
    To add to @paisanco answer, you can also check this post: https://stackoverflow.com/a/39537308/12749132. – Waelmio Dec 29 '20 at 01:37
  • 1
    Please note that this only works for python > 3.6 as the order of the dictionary is maintained when it items are added. – cvanelteren Dec 29 '20 at 01:38
  • @cvanelteren to be pedantic, that was an implementation detail in Python 3.6, but guaranteed in Python 3.7 + – juanpa.arrivillaga Dec 29 '20 at 01:38
  • @cvanelteren really should avoid use builtin names (`reversed`) as variables. – Tom Myddeltyn Dec 29 '20 at 01:40
  • 2
    @juanpa.arrivillaga: To be precise, `reversed` became supported for `dict` and `dict` views as of 3.8. The `list` conversion would be needed on 3.6/3.7. – ShadowRanger Dec 29 '20 at 01:41
  • 1
    @TomMyddeltyn No worries was just a quick inline example. Oddly enough `argparse` also uses builtin-names as variable names and annoys me a lot too. – cvanelteren Dec 29 '20 at 01:46
  • I'm voting to reopen, as the linked question and answer is specific to python 2.7 - this question is about Python in general (and assumed to be Python 3) – Grismar Dec 29 '20 at 01:48
  • 1
    @Grismar [this](https://stackoverflow.com/questions/55911745/python-reverse-dictionary-items-order) is specifically about Python 3. – mkrieger1 Dec 29 '20 at 01:50
  • 1
    @Grismar: The one other people voted for initially was 2.7; the one I dupe hammered to is 3.x (and recent enough to cover the different behaviors from 3.5 and earlier, 3.6-3.7 and 3.8+). Between the two of them the question is definitely answered. – ShadowRanger Dec 29 '20 at 01:51
  • `d[::-1]` would not be pretty because it would imply other kinds of slicing may also work, but dicts can not be efficiently accessed by index and general slices (start, stop, step) seems ambiguous - what should `d[:1]` return? As for a `d.reverse()` method, it's probably just not something that is needed very often so nobody bothered to implement... – wim Dec 29 '20 at 02:22
  • @ShadowRanger fair enough - it's one that keeps coming up, but I'm happy to post the below there as an alternative approach as well. I cannot retract my vote to reopen, it seems? (happy to leave it) – Grismar Dec 29 '20 at 02:30

1 Answers1

1

A very straightforward solution (although it's unclear why you'd need it):

d = {'A':'a', 'B':'b', 'C':'c', 'D':'d'}
dr = {k: d[k] for k in reversed(d)}

print(dr)

Output:

{'D': 'd', 'C': 'c', 'B': 'b', 'A': 'a'}
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • 1
    Note that `d == dr` is still `True`, so dictionaries being "ordered" in Python 3.6+ is a limited concept. `OrderedDict(d) == OrderedDict(dr)` would be `False`. – mkrieger1 Dec 29 '20 at 01:44