There are several ways to create a list in python. Most of us know about for loop. But there is another way which is more pythonic. It is known as List Comprehension.

In for loop, we have to create an empty list first, then append items to the list. But in the list comprehension, we can do it just with a single line of code without creating an empty list.

Here is an example:

Let’s create a list with ‘hello’ using for loop.

char_list = []
for char in 'hello':
    char_list.append(char)

print(char_list)

At first, we created an empty list called char_list. After that we ran a for loop to iterate over each character of the ‘hello’. In the third line, we append each character to the char_list.

The output will look like this:

[ 'h', 'e', 'l', 'l', 'o' ]

let’s rewrite the the above code with list comprehension. We will see how short and nice it is.

char_list = [char for char in 'hello']
print(char_list)

We will get same result here. but it is more readable and elegant. It is also more pythonic.