0

Just completed pset7, gosh it was a long one, took a month!

Am curious as to why we need to have a [0] in front of the list to get the "pure" data?

For instance, when we get back a list while executing something like list=db.execute("Select someData FROM table WHERE id=1")?

If i wanted to use if to use the "pure data" i'd need to assign it to another variable, like so:

createSomeNewVariable=list[0]['someData']

why do i need the [0] before it can work?

Is it because the list is an array? thus i need to get the array number, [0] and then "multiply" the dict?

1

It's because when you run an SQL query with execute(), all the affected records in the database are returned to you, in an array of dictionaries. So you want the first dictionary in that list. But if you gave for example a query that returns all users whose name starts with an S, you might want to use the whole array. Hope that helps.

Also from the doc on query():

Executes SQL statement, possibly with parameters, returning an array of all rows in result set or false on (non-fatal) error.

Happy coding!

2
  • So it's just taking the very first dict in the list. Thanks for the clarification! -- Does it mean if i want the whole array, I'll just leave it without [0] then?
    – nvs0000
    Jun 20 '17 at 17:42
  • 1
    I would say you won't need [0] but might need other methods to play with the array, like iteration to go through every value from your return Jun 27 '17 at 12:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .