#๐Ÿ”’ Edit a dict to a string

38 messages ยท Page 1 of 1 (latest)

true grove
#

Hi i have dicts that i want to be strings and they look like this now:

{'name': 'Bobbaindus', 'donation': 1000},
{'name': 'Liamtheing_', 'donation': 7500},
{'name': 'kruu', 'donation': 893},
{'name': 'fy7', 'donation': 500},
{'name': '1shh', 'donation': 5300}

but i want it to say:

Bobbaindus: 1000
Liamtheing_: 7500
kruu: 893
fy7: 500
1shh: 5300

void wyvernBOT
#

@true grove

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

remote valve
#

do you want to have an unique string at the end?

true grove
#

unique?

#

what is that

remote valve
#

like, all the information in one string

true grove
#

no, i have like
for x in check:
print(x)
and i get one of them at a time

remote valve
#

ok, do you know fstring?

true grove
#

I have heard of it but i dont know how they work

remote valve
#

it is like this:

a = 4
output = f"your number is {a}"
print(output)

# -------

your number is 4```
true grove
#

Okay

remote valve
#

so you could do something like this:```py
l = #list with all your dicts

for dic in l:
print(f"{dic['name']}: {dic['donation']}"```

shrewd spruce
#

I would go so far as to have, and basing it on the given example, py for dic in l: name = dic['name'] donation = dic['donation'] print(f'{name}: {donation}')Purely to keep the f-string itself clean.

#

Which could also be achieved with str.format.

remote valve
#

yeah, it is better

#

but str.format is kind of deprecated

shrewd spruce
#
for dic in l:
    print('{}: {}'.format(dic['name'], dic['donation'])```Seeing it I don't like it.
#

str.format still has some niche uses.

#

Though f-strings can usually do most things.

remote valve
shrewd spruce
#

str.format does templating better than f-strings if you want to pass that template around.

remote valve
#

ah yeah I see

true grove
#

Thanks but i get this error

line 132, in sponsortop
for dic in sponsorcol:
TypeError: 'Collection' object is not iterable

shrewd spruce
#

Post your code.

true grove
#

for dic in sponsorcol:
name = dic['name']
donation = dic['donation']
print(f'{name}: {donation}')

void wyvernBOT
#

Hey @true grove!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
shrewd spruce
#

!paste All of it, please.

void wyvernBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

true grove
#
@bot.slash_command(guild_ids=[1115012773768871958])
async def sponsortop(ctx):
    razkdb = client["razkdb"]
    sponsorcol = razkdb["sponsor"]
    check = sponsorcol.find().sort("donation", -1)
    antaldonationer = 12
    antal = 0
    sidor = [""]
    for x in sponsorcol.find({},{ "_id": 0, "name": 1, "donation": 1 }):
        for dic in sponsorcol:
            name = dic['name']
            donation = dic['donation']
            print(f'{name}: {donation}')
ยดยดยด
shrewd spruce
#

From where does razkdb['sponsor'] hail?

#

client['razkdb']

#

Where does that come from?

#

Where does the Collection type come from?

#

Because I think sponsorcol is a Collection.

#

It's telling you that it's not iterable. You can't for loop through it that way.

#

I think.

void wyvernBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.