Home > Back-end >  How to grab all the name keys once
How to grab all the name keys once

Time:12-31

I wanted to grab all the name keys at one time but all the first dict key aren't the same meaning, e.g., '50','70' etc:

name=data(['50']['name'])# Working

name=data(['50', '70', '220']['name'])
#Not working

How to grab ['name'] from all of the above keys once?

data = {
    "50": {
        "name": "Half-Life: Opposing Force",
        "capsule": "https://cdn.akamai.steamstatic.com/steam/apps/50/header_292x136.jpg?t=1579628243",
        "review_score": 9,
        "review_desc": "Overwhelmingly Positive",
        "reviews_total": "10,931",
        "reviews_percent": 96,
        "release_date": "941443200",
        "release_string": "1 Nov, 1999",
        "platform_icons": "<span class=\"platform_img win\"></span><span class=\"platform_img mac\"></span><span class=\"platform_img linux\"></span>",
        "subs": [
            {
                "id": 32,
                "discount_block": "<div class=\"discount_block discount_block_large\" data-price-final=\"59\"><div class=\"discount_pct\">-80%</div><div class=\"discount_prices\"><div class=\"discount_original_price\">$2.99</div><div class=\"discount_final_price\">$0.59 USD</div></div></div>",
                "discount_pct": 80,
                "price": 59
            }
        ],
        "type": "Game",
        "screenshots": [
            "0000000155.jpg",
            "0000000156.jpg",
            "0000000157.jpg",
            "0000000158.jpg"
        ],
        "review_css": "positive",
        "priority": 0,
        "added": 1574861908,
        "background": "https://cdn.akamai.steamstatic.com/steam/apps/50/page_bg_generated_v6b.jpg?t=1579628243",
        "rank": 954,
        "tags": [
            "FPS",
            "Action",
            "Classic",
            "Sci-fi",
            "Singleplayer"
        ],
        "is_free_game": false,
        "win": 1,
        "mac": 1,
        "linux": 1
    },
    "70": {
        "name": "Half-Life",
        "capsule": "https://cdn.akamai.steamstatic.com/steam/apps/70/header_292x136.jpg?t=1591048039",
        "review_score": 9,
        "review_desc": "Overwhelmingly Positive",
        "reviews_total": "54,635",
        "reviews_percent": 97,
        "release_date": "910549440",
        "release_string": "8 Nov, 1998",
        "platform_icons": "<span class=\"platform_img win\"></span><span class=\"platform_img mac\"></span><span class=\"platform_img linux\"></span>",
        "subs": [
            {
                "id": 34,
                "discount_block": "<div class=\"discount_block discount_block_large\" data-price-final=\"109\"><div class=\"discount_pct\">-80%</div><div class=\"discount_prices\"><div class=\"discount_original_price\">$5.49</div><div class=\"discount_final_price\">$1.09 USD</div></div></div>",
                "discount_pct": 80,
                "price": 109
            }
        ],
        "type": "Game",
        "screenshots": [
            "0000002354.jpg",
            "0000002343.jpg",
            "0000002342.jpg",
            "0000002344.jpg"
        ],
        "review_css": "positive",
        "priority": 0,
        "added": 1574862482,
        "background": "https://cdn.akamai.steamstatic.com/steam/apps/50/page_bg_generated_v6b.jpg?t=1579628243",
        "rank": 910,
        "tags": [
            "FPS",
            "Sci-fi",
            "Action",
            "Multiplayer",
            "1990's"
        ],
        "is_free_game": false,
        "win": 1,
        "mac": 1,
        "linux": 1
    },
    "220": {
        "name": "Half-Life 2",
        "capsule": "https://cdn.akamai.steamstatic.com/steam/apps/220/header_292x136.jpg?t=1591063154",
        "review_score": 9,
        "review_desc": "Overwhelmingly Positive",
        "reviews_total": "101,507",
        "reviews_percent": 97,
        "release_date": "1191999840",
        "release_string": "10 Oct, 2007",
        "platform_icons": "<span class=\"platform_img win\"></span><span class=\"platform_img mac\"></span><span class=\"platform_img linux\"></span>",
        "subs": [
            {
                "id": 36,
                "discount_block": "<div class=\"discount_block discount_block_large\" data-price-final=\"109\"><div class=\"discount_pct\">-80%</div><div class=\"discount_prices\"><div class=\"discount_original_price\">$5.49</div><div class=\"discount_final_price\">$1.09 USD</div></div></div>",
                "discount_pct": 80,
                "price": 109
            }
        ],
        "type": "Game",
        "screenshots": [
            "0000001864.jpg",
            "0000001865.jpg",
            "0000001866.jpg",
            "0000001867.jpg"
        ],
        "review_css": "positive",
        "priority": 0,
        "added": 1574862480,
        "background": "https://cdn.akamai.steamstatic.com/steam/apps/50/page_bg_generated_v6b.jpg?t=1579628243",
        "rank": 912,
        "tags": [
            "FPS",
            "Action",
            "Sci-fi",
            "Classic",
            "Singleplayer"
        ],
        "is_free_game": false,
        "win": 1,
        "mac": 1,
        "linux": 1
    }
}

CodePudding user response:

It sounds like you're looking for a list comprehension over the dictionary's values:

names = [d['name'] for d in data.values()]

CodePudding user response:

You can also use map:

out = list(map(lambda x: x['name'], data.values()))

Output:

['Half-Life: Opposing Force', 'Half-Life', 'Half-Life 2']

CodePudding user response:

Try to:

name=data.filter((x) => ['50', '70', '220'].includes(x)).map((x) => x['name'])
  • Related