How to convert a tuple in a list to a normal list?


language: Python 3.7.0

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"]



Asked By: indigo rother



Use tuple unpacking

response = [('indigo', 'admin')]
data = [*response[0]]

Output: ['indigo', 'admin']

Answered By: Alex P

For this very specific example you can just access the first element of the list a = [('indigo', 'admin')] via your_tuple = a[0] which returns your_tuple = ('indigo', 'admin'). Then this tuple can be converted to a list via list(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:

Answered By: Patrick

you can access the first elem of the origin list [('indigo', 'admin')] (it has only one elem) to get the tuple. then use list function to convert the tuple into list.

Answered By: singulet

You can use:

response=[('indigo', 'admin')]

data=[response[0][i] for i in [0,1]]



['indigo', 'admin']
Answered By: Khaled DELLAL
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.