0

I have code:

r = requests.get(my_url)
d = sorted(r.json().values(), key=lambda x: x['players'], reverse=True)[0:5]

d is:

[{'gamemode': 'roleplay',
  'lang': 'ru',
  'maxplayers': 5000,
  'name': '[RolePlay][Voice] GTA5RP.COM | DownTown | gta5rp.com/discord',
  'peak': 1716,
  'players': 1662,
  'url': 'https://gta5rp.com/'},
 {'gamemode': 'roleplay',
  'lang': 'ru',
  'maxplayers': 5000,
  'name': '[RolePlay][Voice] GTA5RP.COM | VineWood | gta5rp.com/discord',
  'peak': 1578,
  'players': 1568,
  'url': 'https://gta5rp.com/'},
 {'gamemode': 'roleplay',
  'lang': 'ru',
  'maxplayers': 5000,
  'name': '[RolePlay][Voice] GTA5RP.COM | Eclipse | gta5rp.com/discord',
  'peak': 1489,
  'players': 1459,
  'url': 'https://gta5rp.com/'},
 {'gamemode': 'roleplay',
  'lang': 'ru',
  'maxplayers': 5000,
  'name': '[RolePlay][Voice] GTA5RP.COM | StrawBerry | gta5rp.com/discord',
  'peak': 1397,
  'players': 1389,
  'url': 'https://gta5rp.com/'},
 {'gamemode': 'roleplay',
  'lang': 'ru',
  'maxplayers': 3500,
  'name': '[RolePlay][Voice] GTA5RP.COM | Sunrise | gta5rp.com/discord [1.1]',
  'peak': 1337,
  'players': 1323,
  'url': 'https://gta5rp.com/'}]

How I can output data using for like this?

print('Name: ', d["name"]... etc
2
  • Use d[0]["name"] to get the first instance. d is a number of dicts contained within a list. Commented Jul 10, 2021 at 20:13
  • I formatted the data within your question so you can see its structure better. Commented Jul 10, 2021 at 20:16

2 Answers 2

1
for i in d:
    print('Name: ', i["name"])
Sign up to request clarification or add additional context in comments.

Comments

0

d is a list of dictionaries so you can iterate on the list to get every dict and format your output:

for data in d:
    output = f'name: {dic["name"]}, peak: {dic["peak"]}'  # etc
    print(output)

result:

name: [RolePlay][Voice] GTA5RP.COM | DownTown | gta5rp.com/discord, peak: 1716

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.