I came across a strange format of Python code about routine. What is the actual meaning?
def get_values() -> list:
query = "select value from table"
result = db.execute(query)
return [ value[0] for value in result ]
I think the -> list
means this routine returns data type list
but I don't understand how the return (build a list)?
Why it is value[0]
?
CodePudding user response:
If you are using sqlite3 (guessing) execute()
returns a <sqlite3.Cursor>
an iterable containing the tuples satisfying the conditions of your query.
Something like
('2006-01-05', 'BUY', 'RHAT', 100, 35.14)
('2006-03-28', 'BUY', 'IBM', 1000, 45.0)
('2006-04-06', 'SELL', 'IBM', 500, 53.0)
('2006-04-05', 'BUY', 'MSFT', 1000, 72.0)
...
Then
[value[0] for value in result]
creates a list of all the first columns in result
.
['2006-01-05', '2006-03-28', '2006-04-06', '2006-04-05', ...]