How to slice strings inside tuples inside lists?

Question:

I’m working with an SQL database to get a list of primary key ids, however when I print the list, it prints an extra comma along with it. Is there a way to remove or access just the numbers in the list?

Here is what I have:

import mysql.connector

con = mysql.connector.connect(host='localhost',
                              database='database',
                              user='root',
                              password=database_pw)
cursor = con.cursor()
query = """select playerid from nba_players;"""
cursor.execute(query)
playerids = cursor.fetchall()
print(playerids)

Here is the output:

[(20000441,),
 (20000442,),
 (20000443,),
 (20000452,),
 (20000453,),
 (20000455,),
 (20000456,),
 (20000457,),
 (20000466,),
 (20000468,),
 (20000471,),
 (20000474,),
 (20000482,),
 (20000483,),
 (20000485,),
 (20000486,),
 (20000492,),
 (20000497,),
 (20000500,),
 (20000515,),
 (20000516,),
 (20000517,),
 (20000522,),
 (20000539,),
 (20000544,),
...

And here is the output that I’m looking for

[(20000441),
 (20000442),
 (20000443),
 (20000452),
 (20000453),
 (20000455),
 (20000456),
 (20000457),
 (20000466),
 (20000468),
 (20000471),
 (20000474),
 (20000482),
 (20000483),
 (20000485),
 (20000486),
 (20000492),
 (20000497),
 (20000500),
 (20000515),
 (20000516),
 (20000517),
 (20000522),
 (20000539),
 (20000544),
...
Asked By: Matthew Jarrett

||

Answers:

You can use a list comprehension:

[playerid[0] for playerid in playerids]
Answered By: Vladimir Fokow
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.