Create a dictionary by extracting data from SQL. A value (list) is not extracted correctly

Question:

I would like to create a dictionary with values pulled from the database. Then create keys and values. It seems to do everything right except for the last value which is a small list of numbers eg [0, 1, 1, 5]. The Sql code is correct. It’s a Python code problem

I would like to build the dictionary like this for example or something similar:

{'Minnesota-Dallas': ['NHL', 8.1, 9, '15:00', [0, 1, 1, 5],

but the problem is that they don’t extract all the values in the list but only one, for example getting this:

{'Minnesota-Dallas': ['NHL', 8.1, 9, '15:00', 5]

As you can see, the 5 is extracted, but not 0, 1, 1, 5.

The items pulled from the database are row[0] for the key, and then the values are row[1], row[2], row[3], row[4], row[5]. The value I’m having trouble with (the one that should extract [0, 1, 1, 5]) is row[5]. Thank you!

I tried this code:

x = cursor_test.execute('''SELECT Next.team_home||"-"||Next.team_away,
                                            Next.championship, Next.date, Next.current_round, Next.clock,
                                            Results.score_home
                                     FROM Next
                                     INNER JOIN Results
                                     ON Next.team_home = Results.team_home;''')

    test = {}
    for row in x.fetchall():
         test[row[0]] = [row[i] for i in range(1, 6)]

A print(row) inside the loops shows:

('Minnesota-Dallas', 'NHL', 8.1, 9, '15:00', 0) 
('Minnesota-Dallas', 'NHL', 8.1, 9, '15:00', 1) 
('Minnesota-Dallas', 'NHL', 8.1, 9, '15:00', 1) 
('Minnesota-Dallas', 'NHL', 8.1, 9, '15:00', 5)
...
Asked By: Takao貴男

||

Answers:

So, regarding the output of row, you table doesn’t contain a record

('Minnesota-Dallas', 'NHL', 8.1, 9, '15:00', [0, 1, 1, 5])

But N records, for the N values if the list as the end

('Minnesota-Dallas', 'NHL', 8.1, 9, '15:00', 0) 
('Minnesota-Dallas', 'NHL', 8.1, 9, '15:00', 1) 
('Minnesota-Dallas', 'NHL', 8.1, 9, '15:00', 1) 
('Minnesota-Dallas', 'NHL', 8.1, 9, '15:00', 5)

If that isn’t the format you want to store the data in, fix that (and the code you have will work)

Improvement of your code if you fix the format

test = {}
for row in x.fetchall():
    test[row[0]] = row[1:] # equivalent to row[1:6]

# or even better using unpacking
for key, *values in x.fetchall():
    test[key] = values

Solution for the actual data format

test = {}
for row in x.fetchall():
    if row[0] not in test:
        test[row[0]] = list(row[1:])
        test[row[0]][4] = [test[row[0]][4]]  # transform int to list of int
    else:
        test[row[0]][4].append(row[5])

Something better would be to do it in 2 queries

x = cursor_test.execute('''SELECT team_home||"-"||team_away,
                           championship, date, current_round, clock,
                           FROM Next''')

test = {}
for row in x.fetchall():
     test[row[0]] = row[1:]
     q = cursor_test.execute("select score_home from Results where team_home = %s", row[0])
     results = [data[0] for data in q.fetchall()]
     test[row[0]].append(results)
     
Answered By: azro