0

I have the following dict:

All the values are either strings or None or empty or Boolean (True/False)

{
 'id': None,
 'last_login': None,
 'is_superuser': False,
 'is_staff': False,
 'date_joined': '2019-10-06',
 'password': 'pbkdf2_sha256$150000$zXmhAd1NIgI4$1wMGVVDGTSCXZmdFtIoIB/aB3/4nfMn6DnYIuXeRyr8=',
 'last_login_passwd': None,
 'last_login_otp': None,
 'last_password_change': None,
 'first_name': '',
 'last_name': '',
 'email': '',
 'is_active': False,
 'otp_pass_change': '',
 'first_otp_passlogin_create': '',
 'first_otp_otplogin_create': '',
 'about': '',
 'location': '',
 'birth_date': None,
 'first_otp_login_created_date': None,
 'first_pass_login_created_date': None,
 'modified_date': None}

Now i want to see this dict sorted based on values and then based on keys.

{
     'is_active' : FALSE,
     'is_staff' : FALSE,
     'is_superuser' : FALSE,
     'about' : '',
     'email' : '',
     'first_name' : '',
     'first_otp_otplogin_create' : '',
     'first_otp_passlogin_create' : '',
     'last_name' : '',
     'location' : '',
     'otp_pass_change' : '',
     'password' : 'pbkdf2_sha256$150000$zXmhAd1NIgI4$1wMGVVDGTSCXZmdFtIoIB/aB3/4nfMn6DnYIuXeRyr8=',
     'date_joined' : '2019-10-06',
     'birth_date' : None,
     'first_otp_login_created_date' : None,
     'first_pass_login_created_date' : None,
     'id' : None,
     'last_login_otp' : None,
     'last_login_passwd' : None,
     'last_login' : None,
     'last_password_change' : None,
     'modified_date' : None,
}

How can i do that.

I TRIED:

test =     {
     'id': None,
     'last_login': None,
     'is_superuser': False,
     'is_staff': False,
     'date_joined': '2019-10-06',
     'password': 'pbkdf2_sha256$150000$zXmhAd1NIgI4$1wMGVVDGTSCXZmdFtIoIB/aB3/4nfMn6DnYIuXeRyr8=',
     'last_login_passwd': None,
     'last_login_otp': None,
     'last_password_change': None,
     'first_name': '',
     'last_name': '',
     'email': '',
     'is_active': False,
     'otp_pass_change': '',
     'first_otp_passlogin_create': '',
     'first_otp_otplogin_create': '',
     'about': '',
     'location': '',
     'birth_date': None,
     'first_otp_login_created_date': None,
     'first_pass_login_created_date': None,
     'modified_date': None}
sorted(test.items(), key=lambda kv: kv[1], reverse=True)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-41-453676f7d0ed> in <module>
     22      'first_pass_login_created_date': None,
     23      'modified_date': None}
---> 24 sorted(hare.items(), key=lambda kv: kv[1], reverse=True)

TypeError: '<' not supported between instances of 'NoneType' and 'NoneType'
Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • I want to sort and see it. it's not for programming purpose – Santhosh Oct 06 '19 at 22:03
  • sorted(test.items(), key=lambda kv: (str(kv[1]), kv[0]), reverse=True) allows sorting by both value and key, but does not produce your desired sort order. Note: cast value which has different types to string to allow sorting. – DarrylG Oct 06 '19 at 22:07
  • @cricket_007 dictionaries are sorted since CPython 3.6 and officially in all other Python implementiations since Python 3.7 – Boris Verkhovskiy Oct 06 '19 at 22:16

2 Answers2

0

Using str(kv[1]) as below will .

test =     {
 'id': None,
 'last_login': None,
 'is_superuser': False,
 'is_staff': False,
 'date_joined': '2019-10-06',
 'password': 'pbkdf2_sha256$150000$zXmhAd1NIgI4$1wMGVVDGTSCXZmdFtIoIB/aB3/4nfMn6DnYIuXeRyr8=',
 'last_login_passwd': None,
 'last_login_otp': None,
 'last_password_change': None,
 'first_name': '',
 'last_name': '',
 'email': '',
 'is_active': False,
 'otp_pass_change': '',
 'first_otp_passlogin_create': '',
 'first_otp_otplogin_create': '',
 'about': '',
 'location': '',
 'birth_date': None,
 'first_otp_login_created_date': None,
 'first_pass_login_created_date': None,
 'modified_date': None}

print(sorted(test.items(), key=lambda kv: str(kv[1]), reverse=True))
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
kryptoatom
  • 180
  • 1
  • 8
0

According to THIS it's not possible to compare different types and it raises TypeError. You can do this:

test = dict(sorted(test.items(), key=lambda t: (str(t[1]), str(t[0]))))
Hameda169
  • 608
  • 3
  • 9