2

Is there a method to make dictionary lexicographical order ignoring upper and lowercase? Now I know make a dictionary lexicographical order but still confused how to ignore lowercase and uppercase. Here are some codes I have tried.

dic={'subject':'math','name':'','PS':'$'}
for key,value in sorted(dic.items()):
    print(key+'='+value)

Now I have a result that PS=$ is in the first, but I wanna it in the middle, like this:

name=
PS=$
subject=math
An Yan
  • 121
  • 3
  • 11
  • 1
    Dictionaries are never lexiographically ordered; the order is arbitrary prior to Python 3.6/3.7, then the order is based remembering the *insertion order* – Chris_Rands Oct 08 '18 at 10:29
  • 2
    But what you're actually asking it seems is "can I pass a key to `sorted()` to ignore the case", the answer is yes of course (like `sorted(dict.items(), key = lambda x: x[0].casefold())`); and you could feed that to an `OrderedDict` to retain the sorted structure – Chris_Rands Oct 08 '18 at 10:31
  • 1
    If you're only looking to get the ordering during printing, you could try something like `for k, v in sorted(dic.items(), key=lambda x: x[0].lower()):` but obviously, anything that's printed it then thrown away, the original dictionary itself will not have a new order – roganjosh Oct 08 '18 at 10:33
  • @Chris_Rands So the code I write can't make what I print is lexicographical order? – An Yan Oct 08 '18 at 10:34
  • @roganjosh Yeah, you are totally right. But I am a little confused that what the meaning and function of 'key=lambda x: ' is. – An Yan Oct 08 '18 at 10:41
  • 1
    You can look into [lambda functions](https://www.programiz.com/python-programming/anonymous-function). As a super brief explanation, you are saying "I want you to sort by the first item in the tuple - the key - after you cast it to lowercase". The `key` argument will take lambda functions and apply them to allow you to change the default sorting rule – roganjosh Oct 08 '18 at 10:44
  • @roganjosh Very clear! Thank you vert much! – An Yan Oct 08 '18 at 11:05

3 Answers3

1

In-place: Impossible

Reason : Once a key is modified, it won't be possible to retrieve back the original value out of the same old dictionary with the modified key. The only way out here is to store the modified key along with original key.

Create a temporary dictonary, say dic_lex.

Store the modified key (lower-cased) as key and original key as value:

dic={'subject':'math','name':'','PS':'$'}
dic_lex={key.lower() : key for key in dic.keys()}
for key in sorted(dic_lex.keys()):
    print(dic_lex[key]+'='+dic[dic_lex[key]])

prints:

name=
PS=$
subject=math
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
1

In Python 3.6 and 3.7 the dictionaries are insertion ordered, however it is preferable to use OderedDict

Are dictionaries ordered in Python 3.6+?

from collections import OrderedDict

dic={'subject':'math','name':'','PS':'$'}
dic_ord = OrderedDict(sorted(dic.items(), key=lambda t: t[0].lower()))

for key,value in dic_ord.items():
    print(key, '=', value)
Andreas K.
  • 9,282
  • 3
  • 40
  • 45
1

Use sorted with the dictionary constructor

dic = dict(sorted(dic.items(), key=lambda x: x[0].lower()))
# {'name': '', 'PS': '$', 'subject': 'math'}
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20