Solution: Remove Even Integers from a List
Let’s solve the Remove Even Integers From List problem.
Statement
Given a list of integers, lst
, remove all the even integers from the list.
Constraints:
-
lst.length
-
lst[i]
Solution 1: Using list traversal
The naive approach is to traverse the list and add all the odd integers to a separate list. This results in a new list, with all the even integers removed.
Here are the steps of this solution:
Initialize a new list,
odds
.Traverse through
lst
, and for each element in the list:Append the current element to the
odds
list if it’s an odd integer (i.e., not divisible by 2).
Finally, return
odds
after all elements oflst
are traversed.
Let’s look at the illustration below to better understand the solution:
Let’s look at the code for this solution below:
def remove_even(lst):odds = []for number in lst:# Check if the item in the list is not evenif number % 2 != 0:odds.append(number) # // If it isn't even, append it to the odds listreturn oddsdef main():inputs = [[3, 2, 41, 3, 34],[-5, -4, -3, -2, -1],[-1, 2, 3, -4, -10],[1, 2, 3, 7],[2, 4, 6, 8, 10],]for i in range(len(inputs)):print(i + 1, ".\tInput list: ", inputs[i], sep="")print("\n\tFinal list: ", remove_even(inputs[i]), sep="")print("-" * 100)if __name__ == "__main__":main()
Complexity analysis
Let’s look at the time and space complexity of this solution:
Time complexity
The time complexity of the solution is
Space complexity
The space complexity is
Solution 2: Using list comprehension
The solution above can be made further readable using Python list comprehension.
List comprehension is a concise way to create lists in Python. It allows for the generation of new lists by applying an expression to each item in a sequence or iterable, optionally filtering items under a specified condition. The general syntax of a list comprehension is:
[new_expression for item in iterable if condition]
new_expression
: This is the expression that defines how each item in the new list should be constructed from the items in the original iterable.item
: This represents the current element during each iteration through the iterable.iterable
: This is the collection of elements that the list comprehension iterates over.condition
: This is an optional part that if provided, tests each item in the iterable to decide if it should be included in the new list.
Let’s look at the code for this solution below:
def remove_even(lst):# List comprehension to iterate over list and add to a new list if not evenreturn [number for number in lst if number % 2 != 0]def main():inputs = [[3, 4, 40, 31, 32],[-5, -4, -6, -2, -3],[-1, 2, 5, -4, -10],[1],[2, 4, 6, 8, 10],]for i in range(len(inputs)):print(i + 1, ".\tInput list: ", inputs[i], sep="")print("\n\tFinal list: ", remove_even(inputs[i]), sep="")print("-" * 100)if __name__ == "__main__":main()
Complexity analysis
Let’s look at the time and space complexity of this solution:
Time complexity
Using list comprehension enhances code readability. However, the time complexity remains
Space complexity
The space complexity is