Hello I am new to Python and Stackoverflow so please bear with me. I just recently discovered list comprehensions and wanted to "reverse engineer" a specific code to understand it better. In other words, how would the following code look in the regular block format:
return [variable[i:i+10] for i in range(0,100,10)]
http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/ is a great resource, but in general, comprehensions in Python are of the form:
<expression> for <value> in <iterable> [if <criteria>]
So breaking out your example, we have:
variable[i:i+10]irange(0,100,10)The "expanded" form is:
result = []
for <value> in <iterable>:
[if <criteria>:]
result.append(<value>)
(generator, dict, and set comprehensions are all similar)
So taking your example, we get:
result = []
for i in range(0,100,10):
result.append(variable[i:i+10])
return result
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With