4 ms·
But syntax matters! When you're writing stuff all day, a = [_ * 2 for _ in range(10)] is a lot more pleasant than: a = []; for _ in range(10): a.appe
by kstrauser 12y ago
But syntax matters! When you're writing stuff all day,
a = [_ * 2 for _ in range(10)]
is a lot more pleasant than:
a = []; for _ in range(10): a.append(_ * 2)
It also gives Python a lot more information about your actual intent. Suppose "range(10)" were actually "giant_list". Hypothetically, the list comprehension could pre-allocate len(giant_list) elements instead of calling list.append that many times. That's potentially a huge performance win.
You see Python as getting more complex. I see it as getting less complex by giving concise alternatives to common idioms.