0

i am facing the problem which is to parse specific data from list. I have a list sth like :

response_list = [{
                "Name": "Brand",
                "Value": "Smart Planet",
                "Source": "ItemSpecific"
            },
            {
                "Name": "Color",
                "Value": "Yellow",
                "Source": "ItemSpecific"
            },
            {
                "Name": "Type",
                "Value": "Sandwich Maker",
                "Source": "ItemSpecific"
            },
            {
                "Name": "Power Source",
                "Value": "Electrical",
                "Source": "ItemSpecific"
            }]

From the list I should get the Brand name. Every time it will come in different position.How can i get it using list comprehension.Output will be like :

new_list =[{'Brand':'Smart Planet'}]
0

1 Answer 1

1

You have a list of dicts, you want to take only the Name and the Value field of this list.

You can do:

[{item["Name"]: item["Value"]} for item in response_list]

You get:

[{'Brand': 'Smart Planet'}, {'Color': 'Yellow'}, {'Type': 'Sandwich Maker'}, {'Power Source': 'Electrical'}]

Is it what you want?

EDIT

If you only want the brand name, you need to filter:

[{"Brand": item["Value"]} for item in response_list if item["Name"] == "Brand"]
Sign up to request clarification or add additional context in comments.

1 Comment

yes somehow but i only need brand name.I mean the dict which is in first index

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.