Warning: session_start(): open(/tmp/sess_f29057ba6bdb66af3e648a704bef8c01, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Use Python Sort List Of Dictionaries? - LearnShareIT

How To Use Python Sort List Of Dictionaries?

python sort list of dictionaries

Sorting a list in programming is a complex task for beginners. Therefore, today we will show you some ways to use Python sort list of dictionaries using the sorted() function.

How to use Python sort list ofdictionaries?

There are two dimensions to sorting a list. One is ascending, and the other is descending. In this article, we will guide you in the ascending direction. You can do the opposite in the descending direction.

Using the sorted() function and lambda function

We’ve covered the Lamda function’s syntax in a previous post. If you want to read it, you can read it here.

The sorted() function is a built-in function in Python. As the name implies, this function sorts the elements in a list, set, or tuple. About its syntax, you can visit here to see it.

To sort a list of dictionaries, we must pass that list to the first argument of the sorted() function. The second argument is a lambda function that returns the value used to determine the order within the Dictionary. The third parameter is the order of the list. If true, it will sort in descending order. By default, it will sort in ascending order.

It’s a bit confusing, but you’ll understand right away if you look at the example below:

employees = [
  {'name': 'Carlos', 'age': 30},
  {'name': 'Maze', 'age': 19},
  {'name': 'Nayeli', 'age': 24}
]

# Sort employee list by age using sorted() and lambda function
sortedEmployees = sorted(employees, key=lambda employee: employee['age'], reverse=False)
print(sortedEmployees)

Output:

[
  {'name': 'Maze', 'age': 19},
  {'name': 'Nayeli', 'age': 24},
  {'name': 'Carlos', 'age': 30}
]

Using the sorted() function and itemgetter() function

The itemgetter() is used to get items at specific indexes from an iterable.

Syntax:

itemgetter(positions)

Parameter:

  • positions: the indexes or keys to get in an iterable.

This function will return a getter.

We can use the itemgetter() function instead of the lambda function to achieve the same functionality. Since itemgetter() it will return a getter function, it is perfectly valid for the parameter key in sorted().

We can decide which value to sort by passing that value’s key to the itemgetter() function. See the example below to understand.

# Import itemgetter from operator module
from operator import itemgetter

employees = [
  {'name': 'Carlos', 'age': 30},
  {'name': 'Maze', 'age': 19},
  {'name': 'Nayeli', 'age': 24}
]

# Sort employee list by age using sorted() and itemgetter()
sortedEmployees = sorted(employees, key=itemgetter('age'), reverse=False)
print(sortedEmployees)

Output:

[
  {'name': 'Maze', 'age': 19},
  {'name': 'Nayeli', 'age': 24},
  {'name': 'Carlos', 'age': 30}
]

Notice: since itemgetter() is a function in the operator module, we must import it before using it.

Summary

Above are the two most common ways to Python sort list of dictionaries. Many examples show that using itemgetter() is faster than using the lambda function, but we think the difference is negligible. Just choose the way that is most convenient for you.

Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *