Python - First Even Number in List
Last Updated :
06 May, 2023
Sometimes, while working with lists, we can have a problem in which we need to extract certain numbers occurring first time. This can also be even number. This kind of problem is common in day-day and competitive programming. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop This is brute force way in which this task can be performed. In this, we loop through the list and when 1st time even number occurs, we store and break the loop.
Python3
# Python3 code to demonstrate
# First Even Number in List
# using loop
# Initializing list
test_list = [43, 9, 6, 72, 8, 11]
# printing original list
print("The original list is : " + str(test_list))
# First Even Number in List
# using loop
res = None
for ele in test_list:
if not ele % 2 :
res = ele
break
# printing result
print ("The first even element in list is : " + str(res))
Output : The original list is : [43, 9, 6, 72, 8, 11]
The first even element in list is : 6
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using binary search (Sorted list) The binary search can also be employed on list when we have sorted list. In this, we keep looking for smaller even term when we find one and move to rear end in case we don't find one.
Python3
# Python3 code to demonstrate
# First Even Number in List
# using binary search
# Initializing list
test_list = [3, 7, 8, 9, 10, 15]
# printing original list
print("The original list is : " + str(test_list))
# First Even Number in List
# using binary search
res = len(test_list)
low = 0
high = len(test_list) - 1
while low <= high:
mid = (low + high) // 2
if test_list[mid] % 2:
low = mid + 1
else:
res = test_list[mid]
high = mid - 1
# printing result
print ("The first even element in list is : " + str(res))
Output : The original list is : [3, 7, 8, 9, 10, 15]
The first even element in list is : 8
Time Complexity: O(logn), where n is the length of the list test_list
Auxiliary Space: O(1) constant additional space is required
Using Recursion
Python3
# Python3 code to demonstrate
# First Even Number in List
# using recursion
def findfirsteven(lst,i):
if lst[i]%2==0:
return lst[i]
else:
return findfirsteven(lst,i+1)
return -1
# Initializing list
test_list = [43, 9, 6, 72, 8, 11]
# printing original list
print("The original list is : " + str(test_list))
print ("The first even element in list is : " +str( findfirsteven(test_list,0)))
#This code is contributed by Vinay Pinjala
OutputThe original list is : [43, 9, 6, 72, 8, 11]
The first even element in list is : 6
Time Complexity:O(n)
Auxiliary Space: O(n)
Using filter function:
Python3
# Python3 code to demonstrate
# First Even Number in List
# using filter function
# Initializing list
test_list = [43, 9, 6, 72, 8, 11]
# printing original list
print("The original list is : " + str(test_list))
# First Even Number in List
# using filter function
res = list(filter(lambda x: (x % 2 == 0), test_list))[0]
# printing result
print ("The first even element in list is : " + str(res))
OutputThe original list is : [43, 9, 6, 72, 8, 11]
The first even element in list is : 6
Time Complexity : O(n)
Auxiliary Space : O(n)
Method 5 : using a list comprehension.
Python3
# Initializing list
test_list = [43, 9, 6, 72, 8, 11]
# printing original list
print("The original list is : " + str(test_list))
# Finding the first even number in list
even_nums = [num for num in test_list if num % 2 == 0]
if even_nums:
res = even_nums[0]
else:
res = None
# Printing the result
if res:
print("The first even element in list is :", res)
else:
print("There are no even elements in the list.")
OutputThe original list is : [43, 9, 6, 72, 8, 11]
The first even element in list is : 6
The time complexity of this method is O(n) as it involves iterating over the entire list.
The auxiliary space used by this method is O(n) as a new list even_nums is created to store the filtered elements.
Similar Reads
Python program to print even numbers in a list Getting even numbers from a list in Python allows you to filter out all numbers that are divisible by 2. For example, given the list a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], you might want to extract the even numbers [2, 4, 6, 8, 10]. There are various efficient methods to extract even numbers from a li
3 min read
Python | Even Front digits Test in List Sometimes we may face a problem in which we need to find a list if it contains numbers that are even. This particular utility has an application in day-day programming. Letâs discuss certain ways in which this task can be achieved. Method #1: Using list comprehension + map() We can approach this pro
5 min read
Print odd numbers in a List - Python We are given a list and our task is to print all the odd numbers from it. This can be done using different methods like a simple loop, list comprehension, or the filter() function. For example, if the input is [1, 2, 3, 4, 5], the output will be [1, 3, 5].Using LoopThe most basic way to print odd nu
2 min read
Find Median of List in Python The median is the middle value in a sorted list. If the list has an odd number of items, it's the center one, if even, it's the average of the two middle values. It's used to find the center of data. Let's look at the different ways to find the median in Python.Using statistics.median() statistics.m
2 min read
Python - Alternate Minimum element in list Some of the list operations are quite general and having shorthands without needing to formulate a multiline code is always required. Wanting to construct the list consisting of all the alternate elements of the original list is a problem that one developer faces in day-day applications and sometime
4 min read
Print Numbers in an Interval - Python In this article, we are going to learn how to print numbers within a given interval in Python using simple loops, list comprehensions, and step-based ranges.For Example:Input : i = 2, j = 5Output : 2 3 4 5Input : i = 10, j = 20 , s = 2Output : 10 12 14 16 18 20Letâs explore different methods to prin
2 min read