Python Forum
Custom Sorting List of Dictionaries Based on Nested Key and Condition
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Custom Sorting List of Dictionaries Based on Nested Key and Condition
#1
Hi everyone,
I'm working on a small exercise where I need to sort a list of dictionaries representing products. Each product dictionary looks like this:
products = [
    {"name": "Laptop", "price": {"amount": 999, "currency": "USD"}},
    {"name": "Smartphone", "price": {"amount": 499, "currency": "USD"}},
    {"name": "Tablet", "price": {"amount": 299, "currency": "EUR"}},
    {"name": "Monitor", "price": {"amount": 199, "currency": "USD"}},
    {"name": "Camera", "price": {"amount": 599, "currency": "EUR"}},
]
I want to:
Filter only products with currency "USD".
Sort the result by price (ascending).
Print just the names of the sorted products.
Please help me!
Slope Game
Reply
#2
Take a look at: Sorting a Python Dictionary: Values, Keys, and More
deanhystad likes this post
Reply
#3
This would be really easy to do using pandas. Are you allowed to use pandas?

Do it a step at a time. First write a program to filter the products, only those with prices in USD. How do you do that? Sorting should be easy after looking at the link referenced in LARS60+'s response.
Reply
#4
pandas is suggested a lot but it's not really specific
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Nested loops vs list comprehension - pls. help pbsleczkowski 5 3,156 Mar-17-2025, 11:50 AM
Last Post: buran
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 11,775 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  Sorting list - Homework assigment ranbarr 1 3,223 May-16-2021, 04:45 PM
Last Post: Yoriz
  Input validation for nested dict and sorting list of tuples ranbarr 3 5,638 May-14-2021, 07:14 AM
Last Post: perfringo
  nested looping with list cap510 2 3,227 Sep-10-2020, 04:51 AM
Last Post: cap510
  Sorting nested lists in ascending order jszum 2 3,977 May-17-2020, 01:35 PM
Last Post: jefsummers
  Need help comparing totals from list of dictionaries AnOddGirl 1 2,639 Mar-18-2020, 01:17 AM
Last Post: AnOddGirl
  Question about Sorting a List with Negative and Positive Numbers Than999 2 16,722 Nov-14-2019, 02:44 AM
Last Post: jefsummers
  dictionaries and list as values Pippi 6 5,842 Apr-13-2019, 09:05 AM
Last Post: perfringo
  CODE for Bubble sorting an unsorted list of 5 numbers. SIJAN 1 3,414 Dec-19-2018, 06:22 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020