Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
Python failing to check if a value is in a list

So I have this bit of code that selects every row in the column userID, puts the returned value in a dictionary then prints it.

whitelist = []            

with db.cursor() as cursor:
    whitelist_get = "SELECT userID FROM whitelist;"
    cursor.execute(whitelist_get)
    whitelist = str(cursor.fetchall())
    print(whitelist)

And this bit of code checks if the author ID from discord is in that whitelist list.

async def _function(ctx):
    global whitelist
    if ctx.message.author.id in whitelist:
        print("access granted")
    else:
        print("access not granted")

However, when running the script, the code goes over global whitelist then it does not procced in the if statement nor the else statement. It just does nothing. It doesn't print anything either. Am I doing anything wrong?

EDIT: My author ID is in the MySQL table and it should print access granted but it doesn't print anything at all.

EDIT2: Solved by chaning if ctx.message.author.id in whitelist: to if str(ctx.message.author.id) in whitelist:

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The fetchall method returns a sequence of tuples, so you should unpack the tuples to extract the first items first. For efficient lookups, you can make whitelist a set instead:

whitelist = {id for (id,) in cursor.fetchall()}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda