Play with List of dictionaries in Python
Sort a list of dictionaries in Python based on a specific key
list_of_dicts = [
{"folder_name": "C", "other_key": "value1"},
{"folder_name": "A", "other_key": "value2"},
{"folder_name": "B", "other_key": "value3"}
]
sorted_list = sorted(list_of_dicts, key=lambda x: x['folder_name'])
>>> sorted_list
[
{"folder_name": "A", "other_key": "value2"},
{"folder_name": "B", "other_key": "value3"}
{"folder_name": "C", "other_key": "value1"},
]Extract a list of values from a list of dictionaries based on a specific key
list_of_dicts = [
{"folder_name": "C", "other_key": "value1"},
{"folder_name": "A", "other_key": "value2"},
{"folder_name": "B", "other_key": "value3"}
]
folder_names_list = [d['folder_name'] for d in list_of_dicts]
other_keys_list = [d['other_key'] for d in list_of_dicts]
>>> folder_names_list
['C', 'A', 'B']
>>> other_keys_list
['value1', 'value2', 'value3']Flaten List of Lists
Recommended:
Written by - Dishant Sethi
Tags
Enjoyed the blog? If so, you'll appreciate collaborating with the minds behind it as well.
PreviousAchieve Peak Performance in PythonNextHow we develop a custom autoscaling metrics based on number of tasks in the queues?
Last updated