I'm writing a script to populate a list with the last 5 albums frm a list of what's about to play. My query works in terms of retrieving the values, but the list populates with a set of parens and an extra comma added to each returned value. Any ideas on how to get rid of them?

Here's the code:

# Populate 'toosoon' with last 5 albums in current queue

# Further up I import pymysql and then do the connection

toosoon = []

#  Following is my query as written, which is how I run it
cur.execute("select album from songlist where ID in (SELECT SongID from queuelist where sortID in ((select max(SortID)from queuelist), (select max(SortID) -1 from queuelist), (select max(SortID) -2 from queuelist), (select max(SortID) -3 from queuelist), (select max(SortID) -4 from queuelist)))")

# here's the same query but with word wrap to make ir readable
# select album from songlist 
#   where ID in 
#   (SELECT SongID from queuelist 
#       where sortID in (
#           (select max(SortID)from queuelist), 
#           (select max(SortID) -1 from queuelist), 
#           (select max(SortID) -2 from queuelist), 
#           (select max(SortID) -3 from queuelist), 
#           (select max(SortID) -4 from queuelist)
#           )
#    )

for album in cur:
   toosoon.append(album) # adds each album name of returned result to 'toosoon'

cur.close()
conn.close()

print(toosoon)

This returns the list 'toosoon' as:
[('Little Big Men',), ('Hard Believer',), ('Up2zero',), ('Blues for the modern daze',), ('Breathe Deep',)]

but in order to use it later, the 'toosoon' list needs to look like this:
['Little Big Men', 'Hard Believer', 'Up2zero', 'Blues for the modern daze', 'Breathe Deep']

Any ideas on why I'm getting that extra formatting, or how to get it like I need it to be?

thank you,

Recommended Answers

All 2 Replies

Your are taking singleton tuples as result, so you could append album[0] not album at line 24.

Perfect !! Thanks. Seems so easy, once it's pointed out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.