Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Formatting tuples in al list

I used SQLite to get a query from a database. The intention is to rank the most downloaded albums in a top 10. This I have succeeded to do. The result gives me list of tuples. Each tuple is composed of Artist name, Album title and Number of downloads. I want to format this tuple with a ranking from 1 to 10 and tabs in between, like this:

1      Artist name        Album title      Number of downloads

I just can’t seem to figure out how tot change the formatting. I keep comming back to the standard tuple format (Artist name, Album title, Number of downloads).
I can use the zip function to add the ranking from 1 tot 10, but the tuple stays as is

import sqlite3

db = sqlite3.connect("C:\\Users\marlo\Downloads\programs\chinook.db")
cur = db.cursor()
cur.execute("""SELECT artists.name, Title, COUNT(*) as count
            FROM invoice_items
            JOIN tracks ON invoice_items.TrackId = tracks.TrackId
            JOIN albums ON tracks.AlbumId = albums.AlbumId 
            JOIN artists ON albums.ArtistId = artists.ArtistID
            GROUP BY Title
            ORDER BY count DESC
            LIMIT 10""")
Album_ranking = cur.fetchall()
print(*Album_ranking, sep = "\n")

Screenshot Code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Output:

('Chico Buarque', 'Minha Historia', 27)
('Lenny Kravitz', 'Greatest Hits', 26)
('Eric Clapton', 'Unplugged', 25)
('Titãs', 'Acústico', 22)
('Kiss', 'Greatest Kiss', 20)
('Caetano Veloso', 'Prenda Minha', 19)
('The Who', 'My Generation - The Very Best Of The Who', 19)
('Creedence Clearwater Revival', 'Chronicle, Vol. 2', 19)
('Green Day', 'International Superhits', 18)
('Creedence Clearwater Revival', 'Chronicle, Vol. 1', 18)

Screenshot Output

I’m a beginner so I feel like everything I tried thusfar is hardly worth mentioning.
I tried to seperate all the elements by using List comprehension, but that just puts all the elements underneath eacht other
like this:

# formatting
Rank = [*range(1, 11, 1)]
Artist = [ranking[0] for ranking in Album_ranking]
Album = [ranking[1] for ranking in Album_ranking]
Frequency = [ranking[2] for ranking in Album_ranking] 

>Solution :

You need to print row by row, not column by column. Also, you can use enumerate to get a running index like so:

for i, infos in enumerate(cur.fetchall(), start=1):
    print(i, *infos, sep="\t")

Output:

1   Chico Buarque   Minha Historia  27
2   Lenny Kravitz   Greatest Hits   26
3   Eric Clapton    Unplugged   25
4   Titãs   Acústico    22
5   Kiss    Greatest Kiss   20
6   Caetano Veloso  Prenda Minha    19
7   The Who My Generation - The Very Best Of The Who    19
8   Creedence Clearwater Revival    Chronicle, Vol. 2   19
9   Green Day   International Superhits 18
10  Creedence Clearwater Revival    Chronicle, Vol. 1   18
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading