简体   繁体   中英

Python: Sort dictionary keys case-insensitive and return the dictionary in the same format unchanged

I'm new to Python3 and I'm not fully aware of all its useful functions yet.

I have the following dictionary:

my_dict = {'david': ('18', 'Paris', '253-345-5434'), 'Joe': ('19', 'Dubai', '675-353-2345'), 'Luc': ('31', 'Istanbul', '766-673-3451')}

the dictionary keys are strings and each key has a tuple value that contains (age, address, phone number) I tried the sorted method and it returned the following:

['Joe', 'Luc', 'david']

I want it to return ['david', 'Joe', 'Luc'] but I cannot find a way to do so. I need suggestions please!

from collections import OrderedDict
...
print(
    OrderedDict(sorted(my_dict.items()))
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM