0

I have a list in python that looks like this...

[
    {
        "title": "Green Jacket",
        "price": "18",
        "instock": "yes",
    },
    {
        "title": "Red Hat",
        "price": "5",
        "instock": "yes",
    },
    {
        "title": "Green Jacket",
        "price": "25",
        "instock": "no",
    },
    {
        "title": "Purple Pants",
        "price": "100",
        "instock": "yes",
    },
]

I am trying to remove items from the list that have duplicate names, so in the example above the final list would look like this...

[
    {
        "title": "Green Jacket",
        "price": "18",
        "instock": "yes",
    },
    {
        "title": "Red Hat",
        "price": "5",
        "instock": "yes",
    },
    {
        "title": "Purple Pants",
        "price": "100",
        "instock": "yes",
    },
]

Is converting to a dict going to help me in this instance?

jsmitter3
  • 411
  • 1
  • 4
  • 15

1 Answers1

0

This should do what you expect:

lst1 = [
    {
        "title": "Green Jacket",
        "price": "18",
        "instock": "yes",
    },
    {
        "title": "Red Hat",
        "price": "5",
        "instock": "yes",
    },
    {
        "title": "Green Jacket",
        "price": "25",
        "instock": "no",
    },
    {
        "title": "Purple Pants",
        "price": "100",
        "instock": "yes",
    },
]
titles = [item['title'] for item in lst1]
lst2 = [ lst1[ titles.index( title)] for title in set( titles)]
'''

user3435121
  • 633
  • 4
  • 13