List Comprehension

List Comprehension in Python Programming

List comprehension is a unique feature of Python programming language that allows for the creation of a new list by performing the operation on another list. This feature offers a concise way of creating lists and makes code more readable and efficient.

Basic Syntax

The syntax of list comprehension in Python consists of square brackets and the expression, followed by a “for” loop, and one or more “if” clauses.

“`
[expression for item in list if condition]
“`

The expression is the operation that is performed on each item of the list. The “for” loop iterates over each element of the list, while the “if” clause filters the items based on a specific condition.

Examples

Let’s take a look at some examples of list comprehension in action.

Example 1: Squaring the elements of a list.

“`
# Using for loop
a = [1, 2, 3, 4, 5]
b = []

for i in a:
b.append(i**2)

print(b)

# Using list comprehension
a = [1, 2, 3, 4, 5]
b = [i**2 for i in a]

print(b)
“`

Output:
“`
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25]
“`

Example 2: Filtering out the even numbers from a list.

“`
# Using for loop
a = [1, 2, 3, 4, 5]
b = []

for i in a:
if i % 2 == 0:
b.append(i)

print(b)

# Using list comprehension
a = [1, 2, 3, 4, 5]
b = [i for i in a if i % 2 == 0]

print(b)
“`

Output:
“`
[2, 4]
[2, 4]
“`

Example 3: Creating a new list from existing lists.

“`
# Using for loop
a = [1, 2, 3]
b = [“a”, “b”, “c”]
c = []

for i in a:
for j in b:
c.append(str(i) + j)

print(c)

# Using list comprehension
a = [1, 2, 3]
b = [“a”, “b”, “c”]
c = [str(i) + j for i in a for j in b]

print(c)
“`

Output:
“`
[‘1a’, ‘1b’, ‘1c’, ‘2a’, ‘2b’, ‘2c’, ‘3a’, ‘3b’, ‘3c’]
[‘1a’, ‘1b’, ‘1c’, ‘2a’, ‘2b’, ‘2c’, ‘3a’, ‘3b’, ‘3c’]
“`

Nested List Comprehension

List comprehension can also be nested within each other, creating a more complex list from existing lists. The syntax is similar to the basic list comprehension, with additional “for” loops.

“`
[[expression for item in sublist] for sublist in list]
“`

Example: Creating a matrix by multiplying two lists.

“`
# Using for loop
a = [1, 2, 3]
b = [4, 5, 6]
c = []

for i in a:
row = []
for j in b:
row.append(i * j)
c.append(row)

print(c)

# Using nested list comprehension
a = [1, 2, 3]
b = [4, 5, 6]
c = [[i * j for j in b] for i in a]

print(c)
“`

Output:
“`
[[4, 5, 6], [8, 10, 12], [12, 15, 18]]
[[4, 5, 6], [8, 10, 12], [12, 15, 18]]
“`

Advantages of List Comprehension

– Concise and readable code
– Reduced lines of code
– Optimization of the code as it is executed at C level
– Avoidance of creating temporary list and dictionary in many cases.

Conclusion

List comprehension is a powerful feature of Python programming language that offers a concise way to create new lists from existing lists. It makes the code more readable and efficient, reducing the lines of code needed to perform operations on lists. With the ability to perform nested list comprehension, it is suitable for a wide range of applications.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top