Finding the First Even Number in a List using Python


Python has a variety of built-in methods to modify and analyze the text, that contains to find the specific element within them. To solve this problem statement it will use some built-in functions like next(), filter(), and, lambda to find the first even element from the list. The various applications are- data filtering tools, and, content filtering tools.

Let’s see how to take the input of the program to get the result:

The given list,

List_1 = [21, 33, 12, 11, 61, 78]

The final result becomes 12 because it is the first even number from the list.

Syntax

The following syntax is used in the examples-

next()

The next() is an in-built function that iterates the next item in an iterator.

filter()

The filter() method is applied when we need to filter the items based on specific conditions. In simple terms, it allows users to iterate those elements that are extracted to satisfy the condition.

lambda

This lambda function in Python is known as an anonymous function. It can be used when function objects are required.

Using next() function

The program uses the next() function to iterate the individual item one by one from the list.

Example

In the following example, start the program by using a recursive function named even_num_first that accepts the parameter named lst. By using the function return statement it will use the built-in method next() that will find the first even number in the list. The next function returns the next item from an iterator. In this case, the iterator is a generator expression that iterates over the list only for even numbers. If no even number is found in the list, the next function returns -1 as specified by its second argument. Finally, it will use the calling function to pass the list values.

def even_num_first(lst):
  return next((i for i in lst if i%2==0),-1)
print("First even number from the following list:",even_num_first([5, 32, 4, 7, 3, 2]))

Output

First even number from the following list:
32

Using Recursion

The program uses the recursion technique to pass all its parameters that will be used in for loop and condition expression to solve the even number in a list.

Example

In the following example, start the program with a function named first_even which accepts the parameter- list_data to fetch the value through a function call. Then use the for loop where variable i iterate through each value from the variable list_data. Next, use the if-statement under the for loop to check the condition based first even in a list by using > and % operators. Finally, print the result.

def first_even(list_data):
    for i in list_data:
        if i > 0 and i % 2 == 0:
            return i
    else:
        return -1
print("The first even number from the list:",first_even([53, 81, 30, 2, 45, 76, 21]))

Output

The first even number from the list: 30

Using filter() and lambda function

The program uses the built-in method filter to remove those elements that are non-first-even numbers from the list.

Example

In the following example, the program start by initializing the list of integer in the variable 1num_lst and print the list by using the built-in function str(). The program uses the built-in function filter method that two parameter- lambda( that check if the number is even by using mod operator (%)) and num_list( the given input list) and all these processes store it in the variable result. Finally, use the built-in function str that accepts the parameter ‘result’ to get the output.

# Initializing list
num_lst = [73, 67, 71, 83, 10]
# printing original list
print("The given list : " + str(num_lst))
# Using the filter function
result = list(filter(lambda x: (x % 2 == 0), num_lst))
# print the output
print ("The first even integer from the list: " + str(result))

Output

The given list : [73, 67, 71, 83, 10]
The first even integer from the list: [10]

Using for loop

The program uses the for loop to iterate each and individual element from the list and also uses condition expression i.e. if to set the condition for the first even number.

Example

In the following example, the first even number in a list of integers can be found using the function defined in the Python program. The function loops through the list, checking each integer to see if it is even. It returns an even number if one is discovered. It returns None if no even number is found. The function is then called by the program and prints the result.

def even_num_first(numbers):
    for number in numbers:
        if number % 2 == 0:
            return number
    return None
int_list = [35, 30, 11, 4, 17]
result = even_num_first(int_list)

if result is not None:
    print(f"The first even number is present in the list {result}.")
else:
    print("The first even number not present.")

Output

The first even number is present in the list 30.

Conclusion

We learned the various built-in method that helps to solve this problem statement. This type of program helps to identify the trend or pattern in data and organizes sales tracking. By using all the above examples it can also solve a similar program based on the first odd number list.

Updated on: 14-Aug-2023

153 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements