List parsing: the creation of a new list based on the original list
List-Resolved build lists, which can also be implemented with a for loop, have the advantage that the list expression is faster than the For loop.
List Resolution Expressions:
Expression One
[Expression for Iter_var in iterable]
Assigns the value in the Iteration object (iterable) to Iter_var and executes the expression
Cases:
>>> a = [1,2,3,4,5]
>>> B = [i**2 for i in a]
>>> b
[1, 4, 9, 16, 25]
Assigns the value in a to I,i execution i**2, the resulting value generates B
Expression two:
[Expression for Iter_var in iterable if condition]
Removes the value from the Iteration object (iterable), assigns to Iter_var if Contion is satisfied, and executes expression
Cases:
>>> a = [1,2,3,4,5]
>>>c = [i**2 for i in a if I% 2 = = 0 and I >=3]
>>> C
[16]
When the value in a is taken out and the value is even and greater than 3, the i**2 is executed, generating C
List of Python parsing