Python Program to Square Each Odd Number in a List using List Comprehension
Given a list, the task is to write a Python Program to square each odd number in a list using list comprehension.
List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc. A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element. Here we will use list comprehension to make a square of each odd number in a list.
Syntax: newList = [ expression(element) for element in oldList if condition ]
In this example, we will get the square of odd numbers using list comprehension
[1, 9, 25, 49]
In this example, we will get the square of odd numbers using list comprehension.
[121, 529, 169, 9, 1, 9]
Time Complexity: O(n)
Space Complexity: O(n)