Home  >  Article  >  Backend Development  >  What is python list comprehension

What is python list comprehension

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2020-01-06 10:27:198414browse

What is python list comprehension

List comprehensions (also known as list comprehensions) provide a concise and concise way to create lists.

Its structure is an expression in a square bracket, then a for statement, and then 0 or more for or if statements. That expression can be arbitrary, meaning you can put any type of object in the list. The return result will be a new list produced after the expression in the context of the if and for statements has completed.

Execution order of list comprehension: There is a nested relationship between each statement. The second statement on the left is the outermost layer. Go one level to the right in order, and the first statement on the left is the last level.

[x*y for x in range(1,5) if x > 2 for y in range(1,4) if y < 3]

Its execution order is:

for x in range(1,5)
    if x > 2
        for y in range(1,4)
            if y < 3
                x*y

python learning network, free online learning python platform, welcome to follow!

The above is the detailed content of What is python list comprehension. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn