I've been studying list comprehensions and something stopped me for days.
A simple list comprehension has the form
[expression for item in iterable]
the equivalent for
loop is
li=[]
for item in iterable
li.append(item)
If I'm right what generally a list comprehension does is it iterates through the iterable, evaluates the expression for each iteration, then appends it to the list.
Whatever should happen inside the for
loop is written at the beginning of the liscomp.
We can think that in a listcomp Python only allows one expression and for
loop's suit is permitted to have only an if
clause or nested for
loops.
To quote a book I was reading it states that
Since list comprehensions produce lists, that is, iterables, and since the syntax for list comprehensions requires an iterable, it is possible to nest list comprehensions. This is the equivalent of having nested
for … in
loops.
This confused my understanding.
Does this says the reason for having a listcomp like [s+z for s in iterable_1 for z in iterable_2]
Can someone please explain what this says.