List comprehensions and generators in Python

list-comprehensions-and-generators-in-python

In the previous tutorial, we learn about Iteration over Sequences and Collections in Python (With Examples). So, now we are going to discuss List comprehensions and generators in python.

List comprehensions and generators are two powerful features in Python that allow for efficient and concise code.

List comprehensions and generator constructs are used to create sequences of data, but they have different applications and underlying mechanisms.

What is List Comprehensions in Python:

List comprehensions are used to create new lists based on existing ones.

They are a concise way to apply a function or expression to each item in a list and create a new list based on the results.

The syntax for list comprehension is as follows.

Syntax:

new_list = [expression for item in iterable if condition]

Here, expression is the function or operation to apply to each item in the iterable (e.g., a list or range) Item is a variable that represents each item in the iterable, and condition is an optional expression that filters items based on a condition.

For example, the following code creates a list of squares of the first 10 integers:

Code:

squares = [i**2 for i in range(1, 11)]
print(squares)

Output:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

What are Generators in Python:

Generators, on the other hand, are used to create iterable sequences that are generated on-the-fly, rather than being stored in memory.

They are similar to list comprehensions in that they apply a function or expression to each item in a sequence.

They generate the sequence lazily, one item at a time when the sequence is iterated over. This means that generators can be more memory-efficient than lists, especially for large sequences.

The syntax for a generator expression is similar to that of a list comprehension, but with parentheses instead of brackets.

Code:

new_generator = (expression for item in iterable if condition)

For example, the following code creates a generator that yields the squares of the first 10 integers:

Code:

squares_generator = (i**2 for i in range(1, 11))
print(squares_generator)

Output:

<generator object <genexpr> at 0x044919B0>

To iterate over the values generated by a generator, you can use a for loop, just like with a list.

So, the generator only generates one value at a time, you cannot access its elements by index or slice.

So, list comprehensions and generators are powerful constructs in Python that allow for efficient and concise code for creating sequences of data.

List comprehensions are useful for creating new lists based on existing ones, while generators are useful for generating iterable sequences on-the-fly, without storing them in memory. Understanding these constructs can lead to more efficient and readable code in Python.

So, in the next tutorial, we are going to learn about Exception handling (try/except statements) in python. So, make sure to subscribe to us to learn python in an easy way.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x