Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6817883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:06:35+00:00 2026-05-26T21:06:35+00:00

query = ‘select mydata from mytable’ cursor.execute(query) myoutput = cursor.fetchall() print myoutput ((‘aa’,), (‘bb’,),

  • 0
query = 'select mydata from mytable'
cursor.execute(query)
myoutput = cursor.fetchall() 
print myoutput

(('aa',), ('bb',), ('cc',))

Why is it (cursor.fetchall) returning a tuple of tuples instead of a tuple since my query is asking for only one column of data?

What is the best way of converting it to ['aa', 'bb', 'cc'] ?

I can do something like this :

mylist = []
myoutput = list(myoutput)
for each in myoutput:
   mylist.append(each[0])

I am sure this isn’t the best way of doing it. Please enlighten me!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:06:35+00:00Added an answer on May 26, 2026 at 9:06 pm

    This works as well:

    >>> tu = (('aa',), ('bb',), ('cc',))
    >>> import itertools
    >>> list(itertools.chain(*tu))
    ['aa', 'bb', 'cc']
    

    Edit Could you please comment on the cost tradeoff? (for loop and itertools)

    Itertools is significantly faster:

    >>> t = timeit.Timer(stmt="itertools.chain(*(('aa',), ('bb',), ('cc',)))")
    >>> print t.timeit()
    0.341422080994
    >>> t = timeit.Timer(stmt="[a[0] for a in (('aa',), ('bb',), ('cc',))]")
    >>> print t.timeit()
    0.575773954391
    

    Edit 2 Could you pl explain itertools.chain(*)

    That * unpacks the sequence into positional arguments, in this case a nested tuple of tuples.

    Example:

    >>> def f(*args):
    ...    print "len args:",len(args)
    ...    for a in args:
    ...       print a
    ... 
    >>> tu = (('aa',), ('bb',), ('cc',))
    >>> f(tu)
    len args: 1
    (('aa',), ('bb',), ('cc',))
    >>> f(*tu)
    len args: 3
    ('aa',)
    ('bb',)
    ('cc',)
    

    Another example:

    >>> f('abcde')
    len args: 1
    abcde
    >>> f(*'abcde')
    len args: 5
    a
    b
    c
    d
    e
    

    See the documents on unpacking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Query: SELECT DISTINCT ([Equipment List].ID) AS Expr1, [Job Assignments].Job FROM [Equipment List] LEFT JOIN
Query 1 works but query 2 doesn't: Query #1: SELECT * FROM `users` WHERE
Query DECLARE v_vendor_id NUMBER; BEGIN FOR vp_rec IN ( SELECT * FROM vpprocess )
Query: Select * from pu_Products where Part_Number in ('031832','027861', '028020', '033378') and User_Id =
Query: SELECT A.USER_ID, A.ROLE_ID, C.SUBGROUP, MAX(A.STATUS_ID) FROM USER_ROLE A, USER B, ROLE C WHERE
Query: select `r`.`id` as `id` from `tbl_rls` as `r` left join `tblc_comment_manager` as `cm`
Query : select i.Name,ri.Country,ri.State,ri.City from Information as i join ResidenceInformation as ri order by
Query: SELECT StartDate, EndDate, RIGHT(Sector, 1 ) FROM Table1 ORDER BY Right(Sector, 1), StartDate
Query: SELECT * FROM Post ORDER BY Post.rating <-- Here , i want (
Query I'm using: SELECT COUNT(*), SUM(amount) AS amount, FROM_UNIXTIME(added, '%W (%e/%m)') AS daily FROM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.