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

  • SEARCH
  • Home
  • 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 9250853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:32:55+00:00 2026-06-18T10:32:55+00:00

Afternoon, i am trying to retrieve seat numbers from a database using the following

  • 0

Afternoon, i am trying to retrieve seat numbers from a database using the following code

        cur.execute("SELECT * FROM seats")
    while True:
        row = cur.fetchone()
        if row == None:
            break
        print row[0]

But when i do so, it prints out each individual record one per line like so :

A1
A2
B3 etc..

But i want each row to print out with the same letter if that makes sense such as :

A1 A2 A3 A4 A5 A6 A7 A8
B1 B2 B3 B4 B5 B6 B7

But i cant seem to get it like that ? How would i go about doing this?

  • 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-06-18T10:32:56+00:00Added an answer on June 18, 2026 at 10:32 am

    Use the itertools.groupby() tool:

    from itertools import groupby
    
    for letter, rows in groupby(cur, key=lambda r: r[0][0]):
        print ' '.join([r[0] for r in rows])
    

    The groupby() function loops over each row in cur, take the first letter of the first column, and give you a tuples with each (letter, rows) values. The rows value is another iterable, you can loop over that (with a for loop, for example) to list all rows that have that first letter.

    This does rely on the rows being sorted already. If your rows alternate between first letters:

    A1
    A2
    B1
    B2
    A3
    A4
    

    it’ll print those as separate groups:

    A1 A2
    B1 B2
    A3 A4
    

    You may want to add a ORDER BY firstcolumnname ordering instruction to your query to ensure correct grouping.

    This is what I see when I create a test db:

    >>> cur.execute("SELECT * FROM seats ORDER BY code")
    <sqlite3.Cursor object at 0x10b1a8730>
    >>> for letter, rows in groupby(cur, key=lambda r: r[0][0]):
    ...     print ' '.join([r[0] for r in rows])
    ... 
    A1 A2 A3 A4 A5 A6 A7 A8
    B1 B2 B3 B4 B5 B6 B7 B8
    C1 C2 C3 C4 C5 C6 C7 C8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Afternoon all, Using the code below I'm trying to load what is render by
good afternoon,I'm trying to install mysql-5.5.29 source code on ubuntu. but cmake just shows:Configuring
i am getting this error this afternoon while trying to upgrade to oracle apex
Afternoon, I'm trying to implement a hashtable in Java, using K mod 13 as
Good Afternoon, I am trying to use the composite filter but in vain. While
Afternoon, I am trying to return a list of products from my SQL Server
Good afternoon, I'm trying to optimize a source code based on Require.js and Backbone
I've spent a good while this afternoon trying to implement the deserialization of JSON
I have spent the afternoon trying to wrap my mind around how to translate
I have been struggling all afternoon trying to get a simple mailto: tag to

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.