6

I'm trying to use a Class (adns-python) which is expecting a list in the format:

domain_names = ["google.com", "yahoo.com"]

This is working when I declare the list that way manually. However, I'm trying to use a list returned from mysql using python-mysqldb.

When I look at what is being returned from mysql using:

type(mysql_rows) 

This also shows as a list, but when view the result:

print(mysql_rows) 

I can see the list is in the format:

 [('google.com',), ('yahoo.com',)]

I've tried forcing the output to a list again using list(mysql_rows) which didn't work. I've tried parsing the text manually to make it look like the list using:

text_rows = "[" + ", ".join'"%s"' % i for i in mysql_rows = "]"

Which then shows as the correct format, but it is a string not a list so this doesn't work either.

This is my first few days learning python, so I'm sorry if this is an obvious/stupid question.

Thanks

asked Feb 7, 2012 at 23:30

2 Answers 2

6

The list is a list of tuples. A simple

lst = [x for x, in mysql_rows]

should be sufficient.

answered Feb 7, 2012 at 23:34
Sign up to request clarification or add additional context in comments.

2 Comments

Your answer was totally correct too - I selected secretmike because he was a min sooner. Thank you as well.
@direct00: Fair enough :) Glad to help, have fun!
4

mysql returns a list of tuples. Each tuple is a result row in your result set. If you want a list of only the first "column" in the result, try this:

first_column = [x[0] for x in mysql_rows]
answered Feb 7, 2012 at 23:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.