List Comprehension in python allows us to build a list in a different way. Basically, it is one line for loop built inside of brackets.

See this simple example:

Output

You can see both produce the same output but with the list comprehension, it can be done with a single line of code.

Examples:

Output

Below Example uses if statement

Output

Output

Output

Now you should feel comfortable reading and writing basic list comprehensions in Python.

Leave a Reply

Close Menu