discord.py remove every role from list of rols

I am in the process of building a bot that can remove many roles. So far I can get a list of all the roles that I want to remove from the member. But I don’t want to remove all of the roles at once as the length of the list can be anything between 2 and 50. I know that I can remove multiple roles with

member.remove_roles (Role1, Role2, Role3 ...)

but I don’t know how many roles there are in the list so I don’t really know how to elegantly remove all of the roles. Maybe one of you can help me?

Answer

You can use member.edit and pass in a list of roles that the member should have. Sice you want to remove the roles you can just get the current roles and subtract the ones you wanna remove

# list_of_roles = [role1, role2]
await member.edit(roles=set(member.roles)-set(list_of_roles))

We cast the list to a set since you can’t subtract lists, but you can subtract sets

I just checked the code for member.remove_roles and it also uses member.edit, so this is just a faster way to remove roles