language: Python 3.7.0
mysql-connector-python==8.0.31
I’m working on a website and have just implemented a database. The response I’m getting from the database looks like this:
[('indigo', 'admin')]
How do I extract the two values from the tuple in a list and convert it to a list only?
Expected output:
["indigo", "admin"]
Thanks,
indigo
4
Answers
Use tuple unpacking
Output:
['indigo', 'admin']
For this very specific example you can just access the first element of the list
a = [('indigo', 'admin')]
via your_tuple = a[0] which returnsyour_tuple = ('indigo', 'admin')
. Then this tuple can be converted to a list vialist(your_tuple)
.In general it is better not to do these steps in between. I just put them to be more pedagogical. You get the desired result with:
you can access the first elem of the origin list
[('indigo', 'admin')]
(it has only one elem) to get the tuple. then uselist
function to convert the tuple into list.You can use:
Output