python - Please tell this newbie what the difference is between these two list outputs? -


i struggling , have tracked down difference between 2 lists inside code: python debugger:

(pdb) values ['thing1', 'thing2', 'thing3'] (pdb) values2 [['thing1', 'thing2', 'thing3']] 

i do not want double brackets, mean , how rid of them?

'values' creation by: values = ['thing1','thing2','thing3'] 

'values2' creation by:

for report in report.objects.filter(id=id):         values2.append([str(report.name), str(report.subject), str(report.description)]) 

why getting difference , how can values2 values ?

don't think of "double brackets". think of 2 sets of single brackets, 1 inside other. set of brackets means have list. 2 sets of single brackets means have 2 lists. 1 set of single brackets inside means have list inside list.

this because value appended list, because did values2.append([...]). [...] list, appended list; is, put nested list inside values2.

if don't want that, do:

values2.extend([str(report.name), str(report.subject), str(report.description)]) 

extend add each element of list separate element, instead of adding whole list 1 element. (whether work in larger context of program depends on values2.)


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -