Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!