0

From this query...

self.cur.execute("""SELECT pub FROM "20091229"
                 GROUP BY pub ORDER BY pub""")

I get the following list with tuples back.

>>[(1,), (2,), (3,)]

But I need only a list or a list without decimal points in the tuples. Something like these:

[1, 2, 3]

or

[(1), (2), (3)]

Thanks.

1
  • [(1,), (2,), (3,)] - these are not decimal points (commas). These are tuple elements delimiters. Commented Jul 13, 2011 at 13:58

2 Answers 2

1

It will be better if you tell us what module do you use to work with db. Also i am not sure but your query can be optimized to something like this 'SELECT DISTINCT pub FROM "20091229" ORDER BY pub'

I know that nobody here likes reduce but it could be also used here:

from operator import add

reduce(lambda res,x: add(res, x), [(1,), (2,), (3,)])

OR in case there could be only list of single item tuples:

from operator import itemgetter
map(itemgetter(0), [(1,), (2,), (3,)])
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe it is not the best way to solve it but it works. :) Now, I just get a tuple back: (1, 2, 3). Thank you very much.
Thank you, @The_Fox - there are a lot of ways to do things in python and selecting the best is always depends only from you.
1

What about this?

li = [tup[0] for tup in li]

Please note, a tuple get's written like this (1,), because (1) would imply mathematical brackets. Also, it seems you are trying to print a list, consider using a dedicated printing function.

2 Comments

I need a list because i want to use this list in a later sql query as variable and with the decimal points i get an error.
@The_Fox: The problem is is that you are using str(li). Consider proper formatting the list.

Your Answer

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.