-4

I have a Python dictionary in a single file as follows:

{'weight': ['2', '23', '12', '65', '34', '55', '23', '23', '68', '101'], 
'Age': ['35', '23', '14', '39'], 
'Count': ['23', '335', '112', '33', '543']}

I would like to sort just one key/value e.g. Age to be 'Age': ['14', '23', '35', '39'].

The other keys/values to remain unchanged.

How to retrieve a single key and sort its values?

Tks

dot.Py
  • 4,831
  • 4
  • 26
  • 49
bonjih
  • 19
  • 1
  • 3

1 Answers1

2

Pick out the value, sort it & store it back to the same key.

data = {'weight': ['2', '23', '12', '65', '34', '55', '23', '23', '68', '101'],
'Age': ['35', '23', '14', '39'],
'Count': ['23', '335', '112', '33', '543']}

data['Age'] = sorted(data['Age']) # retrieve, sort, store back

print(data)

Result:

{'weight': ['2', '23', '12', '65', '34', '55', '23', '23', '68', '101'], 'Age': ['14', '23', '35', '39'], 'Count': ['23', '335', '112', '33', '543']}
rdas
  • 18,048
  • 6
  • 31
  • 42