Sort Products by Price

Easy ⏱ 8 min 74% acceptance ★★★★☆ 4.4
Write by_price(products) that sorts a list of (name, price) tuples from cheapest to costliest without modifying the original list, using sorted with a lambda key.

Examples

Example 1
Input
by_price([('chair', 5499), ('pen', 10)])
Output
[('pen', 10), ('chair', 5499)]
Explanation

Sorted ascending by the second tuple field.

Constraints

  • Do not mutate the input.
  • Use sorted with key=lambda.

Topics

Lambda & Functionalsorted key

Companies

AmazonFlipkartTCS

Hints

Hint 1

sorted returns a new list.

Hint 2

key=lambda p: p[1].

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.