Formatting tuples in al list

Question:

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:\UsersmarloDownloadsprogramschinook.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

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] 
Asked By: M.KLMN

||

Answers:

I am not sure if i understood correct but maybe sorted is rigth here:

albums = [('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)

def func(element):
     return element[2]

print(sorted(albums, key=func, reverse=True))
Answered By: CNGF

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
Answered By: fsimonjetz
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.