Umar Asghar <mrumarasg...@gmail.com> added the comment:

Please ignore the above shared sample data in the above code and consider this 
one.

this is the exact code that is misbehaving in python 2.7


# Sort nested dictionary by key
# using OrderedDict() + sorted()
from collections import OrderedDict
from operator import getitem
import json

# initializing dictionary
test_dict = {'3' : { 'roll' : 24, 'marks' : 17},
            '2' : {'roll' : 54, 'marks' : 12},
            '1' : { 'roll' : 12, 'marks' : 15}}

# printing original dict
print("The original dictionary : " + str(test_dict))

# using OrderedDict() + sorted()
# Sort nested dictionary by key
res = OrderedDict(sorted(test_dict.items(),
    key = lambda x: getitem(x[1], 'marks')))

print("The sorted dictionary by marks is : " + str(res))
# Output
# The sorted dictionary by marks is : OrderedDict([('2', {'roll': 54, 'marks': 
12}), ('1', {'roll': 12, 'marks': 15}), ('3', {'roll': 24, 'marks': 17})])

res1 = json.dumps(res)

# print result
print("The sorted dictionary by marks is : (json)")
print(json.loads(res1))
# Output
# The sorted dictionary by marks is : (json)
# {u'1': {u'roll': 12, u'marks': 15}, u'3': {u'roll': 24, u'marks': 17}, u'2': 
{u'roll': 54, u'marks': 12}}

----------
Added file: https://bugs.python.org/file48650/bug.py

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38414>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to