0

Here is my data

pricing = [
    {
      "link": "https://www.kohls.com/product/prd-3751773/laura-geller-iconic-baked-sculpting-lipstick.jsp?skuid=75792684",
      "price": "21",
      "stock": true,
      "title": "Laura Geller Iconic Baked Sculpting Lipstick, Red Overfl",
      "seller": "Kohl's"
    },
    {
      "link": "https://www.macys.com/shop/product/laura-geller-beauty-iconic-baked-sculpting-lipstick-cream?ID=5713197&PartnerID=LINKSHARE&cm_mmc=LINKSHARE-_-4-_-41-_-MP441",
      "price": "21.00",
      "stock": true,
      "title": "Laura Geller Beauty Iconic Baked Sculpting Lipstick - Cream",
      "seller": "Macy's"
    },
            {
      "link": "https://www.macys.com/shop/product/laura-geller-beauty-iconic-baked-sculpting-lipstick-cream?ID=5713197&PartnerID=LINKSHARE&cm_mmc=LINKSHARE-_-4-_-41-_-MP441",
      "price": "21.00",
      "stock": true,
      "title": "Laura Geller Beauty Iconic Baked Sculpting Lipstick - Cream",
      "seller": "Wal-Mart.com"
    }
  ]

Below code i am writing make them sort as per the list

retailers = ["Amazon", "Wal-Mart.com", "Target", "CVS",  "Walgreens","Macy’s", "Nordstrom", "MassGenie", "Kohl's", "Kmart"]
data = sorted(pricing, key=lambda x: retailers.index(x['seller']))

error:

ValueError at /api/product/
"Macy's" is not in list

Here i am trying to sort data by seller key as per the list retailers but, i am getting key error while sorting the data. Please have a look where i am making mistake

I am expecting result:

pricing = [
    {
      "link": "https://www.macys.com/shop/product/laura-geller-beauty-iconic-baked-sculpting-lipstick-cream?ID=5713197&PartnerID=LINKSHARE&cm_mmc=LINKSHARE-_-4-_-41-_-MP441",
      "price": "21.00",
      "stock": true,
      "title": "Laura Geller Beauty Iconic Baked Sculpting Lipstick - Cream",
      "seller": "Wal-Mart.com"
    },
    {
      "link": "https://www.macys.com/shop/product/laura-geller-beauty-iconic-baked-sculpting-lipstick-cream?ID=5713197&PartnerID=LINKSHARE&cm_mmc=LINKSHARE-_-4-_-41-_-MP441",
      "price": "21.00",
      "stock": true,
      "title": "Laura Geller Beauty Iconic Baked Sculpting Lipstick - Cream",
      "seller": "Macy's"
    },
    {
      "link": "https://www.kohls.com/product/prd-3751773/laura-geller-iconic-baked-sculpting-lipstick.jsp?skuid=75792684",
      "price": "21",
      "stock": true,
      "title": "Laura Geller Iconic Baked Sculpting Lipstick, Red Overfl",
      "seller": "Kohl's"
    }
  ]

Please have a look

0

3 Answers 3

2

Your code is correct. The problem is "Macy’s" is not the same as "Macy's" (note the different apostrophe character).

Sign up to request clarification or add additional context in comments.

2 Comments

change the. data in retailer's list to reflect the same as dictionary
Just change to '.
1

Your current retailers list shows this way:

  retailers = ["Amazon", "Wal-Mart.com", "Target", "CVS",  "Walgreens","Macy’s", "Nordstrom", "MassGenie", "Kohl's", "Kmart"]

Change it to

  retailers = ["Amazon", "Wal-Mart.com", "Target", "CVS",  "Walgreens","Macy's", "Nordstrom", "MassGenie", "Kohl's", "Kmart"]

All I did was to change to '

Btw, Kohl's looks correct

Comments

0

There where 2 errors in your code

  1. macy’s in retailers list. instead of macy's
  2. true in pricing list. instead of True (Boolean starts with capital Letter in python)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.