Write a Python program to illustrate the following comprehensions: a) List Comprehensions b) Dictionary Comprehensions c) Set Comprehensions d) Generator Comprehensions

 Write a Python program to illustrate the following comprehensions: a) List Comprehensions b)

Dictionary Comprehensions c) Set Comprehensions d) Generator Comprehensions


# a) List Comprehensions

# List comprehensions provide a concise way to create lists.

# A common application is to make new lists where each element is the result of some operation applied to each member of another sequence or iterable,

# or to create a sub-sequence of those elements that satisfy a certain condition.


# Example 1: Create a list of squares of numbers from 0 to 9

squares = [x * x for x in range(10)]

print(f"List of squares (0-9): {squares}")


# Example 2: Create a list of only even numbers from 0 to 9

even_numbers = [x for x in range(10) if x % 2 == 0]

print(f"List of even numbers (0-9): {even_numbers}")



# b) Dictionary Comprehensions

# Dictionary comprehensions are used to create dictionaries using an expression paired with a for loop within curly braces.


# Example 1: Create a dictionary with numbers as keys and their squares as values

square_dict = {x: x * x for x in range(5)}

print(f"Dictionary of squares (0-4): {square_dict}")


# Example 2: Create a dictionary from two lists (using zip)

keys = ['a', 'b', 'c']

values = [1, 2, 3]

combined_dict = {k: v for k, v in zip(keys, values)}

print(f"Dictionary from zipped lists: {combined_dict}")



# c) Set Comprehensions

# Set comprehensions are similar to list comprehensions but create sets, automatically removing duplicate elements.

# They use curly braces, just like dictionary comprehensions, but contain only a single expression before the for loop (no key: value pair).


# Example 1: Create a set of squares, demonstrating automatic removal of duplicates

# Note: the squares of 1 and -1 are both 1, 2 and -2 are both 4, etc.

number_set = {-2, -1, 0, 1, 2}

squares_set = {x * x for x in number_set}

print(f"Set of squares (from {-2, -1, 0, 1, 2}): {squares_set}")


# Example 2: Create a set of unique characters in a string

sentence = "hello world"

unique_chars = {char for char in sentence if char != ' '}

print(f"Set of unique characters in 'hello world': {unique_chars}")



# d) Generator Comprehensions

# Generator comprehensions (or expressions) are similar to list comprehensions but use parentheses instead of square brackets.

# They create generator objects that generate values one by one only when requested (lazy evaluation), which is more memory-efficient for large sequences.


# Example 1: Create a generator for squares of numbers from 0 to 9

# This doesn't compute all values immediately

square_gen = (x * x for x in range(10))

print(f"Generator object (squares 0-9): {square_gen}")


# To use the generator, you can iterate over it (e.g., using a for loop or casting to a list)

print("Iterating through generator values:")

for num in square_gen:

    print(num, end=' ')

print()


# Example 2: Using a generator expression directly with a function that accepts an iterable (like sum())

sum_of_squares = sum(x * x for x in range(5))

print(f"Sum of squares (0-4) using generator expression: {sum_of_squares}")







Comments