<p><br>
> Message: 4<br>
> Date: Sun, 23 Dec 2012 02:48:42 -0500<br>
> From: Mario Cacciatore <<a href="mailto:mariocatch@gmail.com">mariocatch@gmail.com</a>><br>
> To: "<a href="mailto:tutor@python.org">tutor@python.org</a>" <<a href="mailto:tutor@python.org">tutor@python.org</a>><br>
> Subject: [Tutor] List Comprehension Syntax<br>
> Message-ID: <<a href="mailto:50d6b74d.031d650a.3497.ffffad6a@mx.google.com">50d6b74d.031d650a.3497.ffffad6a@mx.google.com</a>><br>
> Content-Type: text/plain; charset="windows-1252"<br>
><br>
> Hey everyone,<br>
><br>
> I am having a very hard time understanding the list comprehension syntax. I've followed the docs and could use some guidance from the fine folks here to supplement my findings. If someone wouldn't mind replying back with an example or two, with some explanation of each part I'd appreciate it.<br>
> -------------- next part --------------</p>
<p>Here's an example that I used a list comprehension for when I was trying to learn it.</p>
<p>I'd leave it to the tutors to say whether this is an appropriate use of it.</p>
<p># search sorted array for integer k</p>
<p>def finder(list, k):<br>
try:<br>
s = sorted([int(i) for i in list])<br>
k = int(k)<br>
except ValueError:<br>
print "Args must be ints. Your list arg was: {0} and your k arg was '{1}'." .format(list, k)<br>
return None<br>
if k in s:<br>
print "Index is: {0}".format(s.index(k))<br>
else:<br>
return -1<br></p>
<p>Hope it helps!<br></p>
<p>Malcolm Newsome</p>
<p>P.S. I hope the formatting is ok...I'm responding from my phone.</p>