So I have a dictionary, say:
myDict = {"145":1, "Kittens":2, "apples":1, "trees":2}
and using pprint(myDict, ...)
, I get:
{'145': 1,
'Kittens': 2,
'apples': 1,
'trees': 2}
Instead, I'd like to ignore the capital K's apparent order priority and get:
{'145': 1,
'apples': 1,
'Kittens': 2,
'trees': 2}
Do I have to use the PrettyPrinter module? Is there a hidden pprint argument? Or is there another solution entirely? My dict keys aren't any more complicated than this. Thanks.
Without imports, simple version:
def printdict(myDict):
print('{')
for a,b in sorted(myDict.items(),key = lambda tuple : tuple[0].lower()):
print("'"+str(a)+"'"+" : "+str(b))
print('}')
Improved version according to PM 2Ring ( this will work for anything ):
def printdict(myDict):
print('{')
for a,b in sorted(myDict.items(),key = lambda t : t[0].lower()):
print(' {!r}: {!r},'.format(a, b))
print('}')