user13965463
user13965463

Reputation:

How to alter output or filter output to display what you want

This is the code that displays output that I would like to alter:

import json
from collections import OrderedDict


with open('users.json', 'r') as file:
    data = json.load(file)

ordered_data = OrderedDict(sorted(data.items(), key=lambda k: -k[1]['balance']))

first = ordered_data.popitem(last=False)
print(first)

Ouput:

('<@!702221444796383454>', {'balance': 90})

I would like the code to output this instead:

702221444796383454 90

I have tried "".join() and I tried googling things to try and find a solution but I could not find anything.

Upvotes: 1

Views: 30

Answers (2)

Pavan Chandaka
Pavan Chandaka

Reputation: 12731

Conceptually, the popitem returns a tuple.

('<@!702221444796383454>', {'balance': 90}) is the tuple returned by popitem in your case. And the returned tuple is stored in the variable "first".

So first[0] will return the first element and first[1] will return the second element.

Then tweak them accordingly, in the way you want.

Upvotes: 0

Jack
Jack

Reputation: 569

After you get the. variable first you can extract the bits of information you want into a string, and if the first part is always in that format you can splice it up.

output =f"{first[0][3:-1]} {first[1]['balance']}"
print(output)

Upvotes: 1

Related Questions