Python Forum
Help with list thing please - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help with list thing please (/thread-17652.html)



Help with list thing please - Yeyzon - Apr-19-2019

So I'm trying to get both of the ["name"] in this list and have them put into a txt file

[{'id': 2124447617, 'name': 'Welcome To The Generator', 'description': 'Thanks for joining our generator', 'enabled': True, 'iconImageId': 2687851451, 'awarder': {'id': 2101105173, 'type': 'Place'}, 'statistics': {'pastDayAwardedCount': 163, 'awardedCount': 41664, 'winRatePercentage': 1.0}, 'created': '2018-12-28T20:05:03.977Z', 'updated': '2018-12-28T20:05:03.977Z'}, {'id': 2124456449, 'name': '1 Minute Played', 'description': 'very cool 1 min', 'enabled': True, 'iconImageId': 2894312587, 'awarder': {'id': 2101105173, 'type': 'Place'}, 'statistics': {'pastDayAwardedCount': 148, 'awardedCount': 20791, 'winRatePercentage': 1.0}, 'created': '2019-02-24T23:29:37.117Z', 'updated': '2019-02-25T02:21:27.083Z'}]
How would I be able to?

sorry im bad at python


RE: Help with list thing please - snippsat - Apr-19-2019

It look like a part coming from JSON,and then it's not normal to start with list.
Anyway here format with black,then is easier to see the structure.
lst = [
    {
        "id": 2124447617,
        "name": "Welcome To The Generator",
        "description": "Thanks for joining our generator",
        "enabled": True,
        "iconImageId": 2687851451,
        "awarder": {"id": 2101105173, "type": "Place"},
        "statistics": {
            "pastDayAwardedCount": 163,
            "awardedCount": 41664,
            "winRatePercentage": 1.0,
        },
        "created": "2018-12-28T20:05:03.977Z",
        "updated": "2018-12-28T20:05:03.977Z",
    },
    {
        "id": 2124456449,
        "name": "1 Minute Played",
        "description": "very cool 1 min",
        "enabled": True,
        "iconImageId": 2894312587,
        "awarder": {"id": 2101105173, "type": "Place"},
        "statistics": {
            "pastDayAwardedCount": 148,
            "awardedCount": 20791,
            "winRatePercentage": 1.0,
        },
        "created": "2019-02-24T23:29:37.117Z",
        "updated": "2019-02-25T02:21:27.083Z",
    },
]
>>> lst[0]['name']
'Welcome To The Generator'
>>> # Then both name would be
>>> [i['name'] for i in lst]
['Welcome To The Generator', '1 Minute Played']
Try to put in .txt file yourself,post back if have trouble.