Can someone please help me to understand list comprehensions in Python?
I'm working my way through the MIT Gentle Introduction to Python and I'm stuck on the list comprehensions section.
The first exercise I get.
Code:
# ********** Exercise 2.10 **********
oneten=[1,2,3,4,5,6,7,8,9,10]
cubes=[x**3 for x in oneten]
# Test Cases
##### YOUR CODE HERE #####
print cubes
but the next is to use list comprehension to generate all possible results of two coin tosses (hh, ht, th, tt)
So far I have this, which generates only "hh , tt" and a headache...
Code:
flips=["h", "t"]
allresults=[x+x for x in flips ]
print allresults
Do I use a loop inside a list comprehension statement, or am I completely on the wrong track?