Prodinit Software Solutions
LinkedinGithub
  • Prodinit's Engineering Blog
  • aws
    • Ways to delete AWS ECR images
    • Enable Cloudwatch Alarm and SNS Topic for AWS Billing Alert
    • A-Z of AWS VPC and other services - with Terraform
    • How Internet Works?
    • How to download/view code running in your lambda functions?
  • backend engineering
    • What is idempotency?
  • databases
    • Database Optimisation - Indexing vs Sharding with Postgres and Django ORM examples
  • devops
    • Docker Best Practices
    • Docker Networking - Bridge vs Host vs Overlay
    • A comparision between multistage build and singlestage build in Docker
    • Things to remember before building your first blue/green deployment in Kubernetes
    • How to export env variables in circleci? (You wont find this in circleci documentation)
  • frontend engineering
    • Host your static website with s3, CloudFront, Route53, and domain from godaddy in 4 easy steps
  • product management
    • You'll fail as a lead developer, here's why ...
  • python
    • Achieve Peak Performance in Python
    • Play with List of dictionaries in Python
    • How we develop a custom autoscaling metrics based on number of tasks in the queues?
  • Contact Us
    • Who are we?
    • Work with us.
Powered by GitBook
On this page
  • Sort a list of dictionaries in Python based on a specific key
  • Extract a list of values from a list of dictionaries based on a specific key
  • Flaten List of Lists
  • Enjoyed the blog? If so, you'll appreciate collaborating with the minds behind it as well.
  1. python

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

list_of_lists = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]

flattened_list = [item for sublist in list_of_lists for item in sublist]

>>> flattened_list
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Recommended:

from itertools import chain

list_of_lists = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
flattened_list = list(chain.from_iterable(list_of_lists))

>>> flattened_list
[1, 2, 3, 4, 5, 6, 7, 8, 9]

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 1 year ago

Written by -

Dishant Sethi