-1

I have a dictionary like this:

d = {
   'yellow': ['C','A','F'], 
   'blue': ['D','A'],
   'brown': ['G','Z'],
   'red': ['F','T'],
   'green': ['Z','A']
}

I'm trying to get output like this:

{
    'blue': ['A','D'],
    'brown': ['G','Z'],
    'green': ['A','Z']}
    'red': ['F','T'],
    'yellow': ['A','C','F']
} 

I'm trying to sort key and value alphabetically. I've tried using modules to do this, but how to do it without using modules?

timgeb
  • 76,762
  • 20
  • 123
  • 145
  • Keys in dictionary don't have any order. Can you please elaborate for what purpose you need the output? You want to save this output to a file or you just want to print output like this in console? – Amit Vikram Singh Apr 06 '21 at 08:36
  • @AmitVikramSingh In Python 3 they do. – FMc Apr 06 '21 at 08:37
  • @FMc Oh, I was not aware of that. If we are not using OrderedDict then what is the order of keys in a dictionary? – Amit Vikram Singh Apr 06 '21 at 08:38
  • 1
    @FMc in Python 3.7 and up (from 3.5 as a CPython implementation detail) they do preserve insertion order, but they're still not _semantically_ ordered data structures (you can't ask for the "first" or "last" item and order doesn't matter in comparisons, for example, unlike with `OrderedDict`s). – jonrsharpe Apr 06 '21 at 08:38
  • @jonrsharpe Fair point, but perhaps not a binding distinction here. – FMc Apr 06 '21 at 08:40
  • @jonrsharpe If normal dictionary preserves insertion order then OrderedDict is obsolete in python 3.7+? – Amit Vikram Singh Apr 06 '21 at 08:42
  • 2
    @AmitVikramSingh no; again, a vanilla dictionary is still not *semantically* ordered. `dict(a=1, b=2) == dict(b=2, a=1)` whereas `OrderedDict(a=1, b=2) != OrderedDict(b=2, a=1)`, for example, and there are two methods from [`OrderedDict`](https://docs.python.org/3/library/collections.html#collections.OrderedDict) (`popitem` and `move_to_end`) vanilla dictionaries don't have. – jonrsharpe Apr 06 '21 at 08:44

3 Answers3

3
>>> d = {'yellow': ['C','A','F'],
...  'blue': ['D','A'],
...  'brown': ['G','Z'],
...  'red': ['F','T'],
...  'green': ['Z','A']}
>>>
>>> {key: sorted(val) for key, val in sorted(d.items())}
{'blue': ['A', 'D'], 'brown': ['G', 'Z'], 'green': ['A', 'Z'], 'red': ['F', 'T'], 'yellow': ['A', 'C', 'F']}
atin
  • 985
  • 3
  • 11
  • 28
3

You'll need Python 3.7+ for dicts to remember their key insertion order. Then you can use:

dict(sorted((k, sorted(v)) for k, v in d.items()))

edit: I also like this one.

{k: sorted(d[k]) for k in sorted(d)}
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • So we don't need to use OrderedDict in python3.7+ for preserving the insertion order? – Amit Vikram Singh Apr 06 '21 at 08:43
  • 1
    @AmitVikramSingh [correct](https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6), but there are still differences between regular and ordered dicts (see the link). – timgeb Apr 06 '21 at 08:44
1

try this code:

data = {'yellow': ['C', 'A', 'F'],
    'blue': ['D', 'A'],
    'brown': ['G', 'Z'],
    'red': ['F', 'T'],
    'green': ['Z', 'A']}

res = {} # New Sorted Dict
sorted_keys = sorted(data.keys()) # sorted keys

for key in sorted_keys:
    res[key] = sorted(data.get(key)) # Sorted values For key
print(res)
George Imerlishvili
  • 1,816
  • 2
  • 12
  • 20