I was just playing with list comprehensions and came across this :
h = [ b for b in range(1, 9) for k in range(b, b*10) if k%2==0 for j in range(2*k, k*k)]
Expected Result:
h = [1, 2, 3, 4, 5, 6, 7, 8]
Actual Result is not as expected, contains len(h) = 196000
items.
Please explain how this works ?
Why do you think this is the expected result?
Your code is equivalent to:
So, for each number from 1 to 8, it will append it many times to the list
You can also see how many times each number is added with the help of groupby: