Warren Daza
Warren Daza

Reputation: 41

Python, sorting a dictionary list

I'm having trouble sorting out a dictionary key/value. Basically, this is what I'm trying to achieve: (This works)

>>> dict = {}
>>> dict['pens'] = ['234sdf, gjjhj', 'asd123, fghtert', '652efg, vbn456h']
>>> dict['pens'] = sorted(dict['pens'])
>>> print('Sorted: ', dict['pens'])
Sorted:  ['234sdf, gjjhj', '652efg, vbn456h', 'asd123, fghtert']

Now in my actual script, I get an error:

>>> grpProfile['students'] = sorted(grpProfile['students'])
TypeError: unorderable types: NoneType() < NoneType()
type(grpProfile['students']) # returns list
type(grpProfile['students'][0] )# returns string

This also displays the list of strings.

for s in grpProfile['students']:
  print(s)

What could be wrong with my code? I'm completely lost. Every test I've done is working.

Upvotes: 0

Views: 74

Answers (1)

Duca d&#39;Auge
Duca d&#39;Auge

Reputation: 53

As ajcr has correctly pointed out, it seems that at least one element in your list is NoneType() type, i.e. you have a None object (maybe a variable you have not set).

Get rid of them, and then sort your list of values. To create a list without instances of that type:

[x for x in list if x is not None]

Upvotes: 2

Related Questions