Sort a dict with first based on value and then based on key if value evaluates to False.

In [19]:
import operator as op
In [1]:
data = {
 '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
}
In [4]:
expected = {
     '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,
}
In [16]:
false_or_empty_keys = {key for key in data.keys() if data[key] in (False, '')}
In [18]:
valid_values_keys = set(data.keys()).difference(false_or_empty_keys)
In [31]:
false_or_empty = {key: data[key] for key in sorted(false_or_empty_keys)}
head = sorted(false_or_empty.items(), key=lambda x: 0 if x[1] == False else 1)
head
Out[31]:
[('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', '')]
In [60]:
valid_values = {key: data[key] for key in valid_values_keys}
not_none_valid_values = sorted(
    [(key, data[key]) for key in valid_values_keys if data[key] is not None],
    key=op.itemgetter(1),
    reverse=True,
)
not_none_valid_values
Out[60]:
[('password',
  'pbkdf2_sha256$150000$zXmhAd1NIgI4$1wMGVVDGTSCXZmdFtIoIB/aB3/4nfMn6DnYIuXeRyr8='),
 ('date_joined', '2019-10-06')]
In [61]:
none_valid_values = sorted(((key, value) for key, value in valid_values.items() if valid_values[key] is None), key=op.itemgetter(0))
none_valid_values
Out[61]:
[('birth_date', None),
 ('first_otp_login_created_date', None),
 ('first_pass_login_created_date', None),
 ('id', None),
 ('last_login', None),
 ('last_login_otp', None),
 ('last_login_passwd', None),
 ('last_password_change', None),
 ('modified_date', None)]
In [62]:
dict((*head, *not_none_valid_values, *none_valid_values))
Out[62]:
{'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': None,
 'last_login_otp': None,
 'last_login_passwd': None,
 'last_password_change': None,
 'modified_date': None}