3

I just want to sort by first_name this list in python

list = [ { "profile" :  { "first_name" : "a", "last_name" : "b" } } ,
         { "profile" :  { "first_name" : "c", "last_name" : "d" } } ,
         { "profile" :  { "first_name" : "e", "last_name" : "f" } } ]
1
  • 7
    best not to use list as a variable name Commented Nov 5, 2011 at 2:09

2 Answers 2

9

This should do it:

>>> sorted(lst, key=lambda x: x['profile']['first_name'])
Sign up to request clarification or add additional context in comments.

Comments

7

To sort the list itself, use:

lst.sort(key=lambda x: x['profile']['first_name'])

To keep lst unsorted and return a sorted list use:

sorted(lst, key=lambda x: x['profile']['first_name'])

1 Comment

@AtulMakwana - You many have used .sort() syntax instead of sorted()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.