Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display only True values in a dictionary

I am trying to display the permissions of a member from a dict as in

permdict = {
                    'Administrator': f'{member.guild_permissions.administrator}',
                    'Ban Members': f'{member.guild_permissions.ban_members}',
                    'Kick members': f'{member.guild_permissions.kick_members}'}

at this moment this will return something like :

{'Administrator': 'True', 'Ban Members': 'True', 'Kick members': 'True'}

or false if the user doesn’t have these permissions, my goal is to be able to filter the keys which have True values, so if the user have just x/3 permissions (the dictionary will be bigger, but i don’t want to proceed further unless i am able to solve my problem), to show just the Key with True value and if the user has none of the permissions in the dictionary it will return nothing or a string ex You have no moderation roles to dispaly and if some of the perms are present to display them just by key Administrator, Kick members etc. . . I didn’t worked too much with bools inside dictionaries so i hoped i can find a solution here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i also tried to combine it with .join:

 permdict = {'Administrator': f'{member.guild_permissions.administrator}',
                    'Ban Members': f'{member.guild_permissions.ban_members}',
                    'Kick members': f'{member.guild_permissions.kick_members}'}

 permf = ", ".join(permdict) ​

And the result was what i desired

Administrator, Ban Members, Kick members ​

but the problem was that even if the user didn’t had these perms they will still be displayed. I feel like i’m quite close to find a solution but i can’t figure it out

>Solution :

Use a generator with a condition:

permf = ", ".join(key for key, value in permdict.items() if value == "True")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading