1

The task:

Write a function named "one_column" that doesn't take any parameters and returns a list. There is a database saved in a file named "example.db" containing a table named "understanding" with columns "carrier", "describe", and "timing" each storing integer values. Return a list containing only the "timing" value of each record in the table.

My code so far:

import sqlite3

conn = sqlite3.connect('example.db')
c = conn.cursor()
conn.commit()
def one_column():
    total = c.execute('SELECT timing FROM understanding')
    return list(total)

My code is returning

[[30], [63], [63], [7], [62], [79], [1], [54], [20], [45], [93], [45], [38], [30], [84], [64]].

but the expected result is

[30, 63, 63, 7, 62, 79, 1, 54, 20, 45, 93, 45, 38, 30, 84, 64].

How do I get rid of the nested lists?

timgeb
  • 76,762
  • 20
  • 123
  • 145

1 Answers1

0

If your inner lists only contain one element each, there's a very simple list comprehension that unnests the list.

>>> lst = [[30], [63], [63], [7], [62], [79], [1], [54], [20], [45], [93], [45], [38], [30], [84], [64]]
>>> [x for x, in lst]
[30, 63, 63, 7, 62, 79, 1, 54, 20, 45, 93, 45, 38, 30, 84, 64]

This unpacks the sequences of length one in lst.

If your inner lists can have more than one element, see this question.

timgeb
  • 76,762
  • 20
  • 123
  • 145