Syntax

Python List Comprehension Syntax

In Python, list comprehension: builds a new list from an expression — [x * 2 for x in nums if x > 0]

Quick Answer

Syntax
[x * 2 for x in nums if x > 0]
PythonSyntax

List Comprehension

Code example
[x * 2 for x in nums if x > 0]

Builds a new list from an expression.

Learn More

How to list comprehension in Python

  1. Open your Python file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Python List Comprehension Syntax

Use [x * 2 for x in nums if x > 0].

Related Syntax References

Frequently asked questions

What is the Python syntax for list comprehension?

Use this pattern: [x * 2 for x in nums if x > 0]

Can I copy this Python code example?

Yes. Use Copy code and adapt the example to your program.

Is this Python syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify