I'm having a hard time with the final written exercise. I've read about list comprehensions, but something isn't clicking for me. Any help is appreciated.
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
they are just another way of producing a new list.
if a = [1,2,3,4,5,6,7,8], we could produce a list of the squared elements of a:
sqrs =  # an empty list
for x in a: # for each element of a
b.append(x*x) # add x*x to the list b
list comprehensions just give you a more concise mechanism to do it:
b = [ x*x for x in a] # b is the same list as produced above.
if we have some function def
return x*x*x + 17
we can produce a list of f(i) for each i in a
b = [f(i) for i in a] which is the list [f(1),f(2)...f(8)]
we can even add a condition:
b = [x for x in a if x % 2 == 0]
this is the same as:
b = 
for x in a:
if x % 2 == 0:
a list comprehension is only a shorter (different) way of doing it, despite the name,
nothing really new. play with a few in idle. a = [1,2,3,4], b = [x/2 for x in a],
and print b. I hope I haven't made things worse...
Very nice concise answer.
Thank you for asking the question Susan. That gave snark the opportunity to answer.