Home > Enterprise >  extract values from a list on python
extract values from a list on python

Time:03-31

Im programming a webapp on python and on one part of the code i have to take a value from a database. this value is on a list but this list is from a length of one. How can i take it out without making a for that will only run once?

btw this is the code

if not password and email:
    flash("Please enter an email and a password to login", category="error")
else:
    con = sqlite3.connect("DB.db")
    cur = con.cursor()
    cur.execute("SELECT * FROM users WHERE email = ?", (email,))
    user = cur.fetchall()
    con.close()
    if not user:
        flash("This account doesn't exist")
    else:
        for User in user: #<-- this for
            pass

CodePudding user response:

If it has only a single element in it, then print the whole list:

print(*user)

or use the first and only element in the list

user[0]

CodePudding user response:

If you know that the list always has one element you can easily access the first element with list[0]

(our_user,) = user[0]

Edit: Thx @jedwards

  • Related