-1

I have a dictionary that is not sorted alphabetically that looks like this:

dict = {'zebra': ['file1.txt'], 'alpha': ['file2.txt', 'file3.txt'], ...........}

I want to sort it alphabetically so the output is:

dict ={'alpha': ['file2.txt', 'file3.txt'],'zebra': ['file1.txt'],  ...........}

But from what I've seen online so far, their code for ordering it alphabetically loses the properties of a dictionary and either returns a list or it uses OrderedDict which is not the type I want.

A suggestion that made it a list was


sorteddict=sorted(dict.keys(), key=lambda x:x.lower())

But obviously that is not what I'm looking for

Can I somehow use sort on a dictionary while keeping its properties?

martineau
  • 112,593
  • 23
  • 157
  • 280
David R
  • 13
  • 5

1 Answers1

1

You should not name your variable dict otherwist you won't be able to use the dictionary constructor.

dict1 = dict(sorted(dict1.items())
Alain T.
  • 34,859
  • 4
  • 30
  • 47