How does this single line for loop work in Python? [list comprehension] -


here code sample:

from django.shortcuts import render_to_response import mysqldb  def book_list(request):     db = mysqldb.connect(user='me', db='mydb', passwd='secret', host='localhost')     cursor = db.cursor()     cursor.execute('select name books order name')     names = [row[0] row in cursor.fetchall()]     db.close()     return render_to_response('book_list.html', {'names': names}) 

the line in particular is:

names = [row[0] row in cursor.fetchall()] 

i want understand, how line in particular, understand shorthand way of doing things, provide how long version like?

that line list comprehension. here 'long' version.

names  = []  row in cursor.fetchall():     names.append(row[0]) 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -