Open In App

Python – Find the distance between first and last even elements in a List

Last Updated : 16 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a List, write a Python program to find the span of even elements in list, i.e distance between first and last occurrence of even element.

Examples:

Input : test_list = [1, 3, 7, 4, 7, 2, 9, 1, 10, 11] 
Output : 5 
Explanation : Even elements begin at 4 and end at 10, spanning 5 indices.

Input : test_list = [1, 3, 7, 4, 7, 2, 9, 1, 1, 11] 
Output : 2 
Explanation : Even elements begin at 4 and end at 2, spanning 2 indices. 

Method #1: Using list comprehension

In this we get all the indices of all even elements using list comprehension and then perform difference of first and last index of matched elements in list.

Python3




# Python3 code to demonstrate working of
# Even elements span in list
# Using list comprehension
 
# initializing Matrix
test_list = [1, 3, 7, 4, 7, 2, 9, 1, 10, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting even indices
indices_list = [idx for idx in range(
    len(test_list)) if test_list[idx] % 2 == 0]
 
# getting difference of first and last occurrence
res = indices_list[-1] - indices_list[0]
 
# printing result
print("Even elements span : " + str(res))


Output

The original list is : [1, 3, 7, 4, 7, 2, 9, 1, 10, 11]
Even elements span : 5

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #2 : Using filter() + lambda

In this, we perform task of getting indices of elements using filter() and lambda. 

Python3




# Python3 code to demonstrate working of
# Even elements span in list
# Using filter() + lambda
 
# initializing Matrix
test_list = [1, 3, 7, 4, 7, 2, 9, 1, 10, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting even indices
indices_list = list(
    filter(lambda x: test_list[x] % 2 == 0, range(len(test_list))))
 
# getting difference of first and last occurrence
res = indices_list[-1] - indices_list[0]
 
# printing result
print("Even elements span : " + str(res))


Output

The original list is : [1, 3, 7, 4, 7, 2, 9, 1, 10, 11]
Even elements span : 5

Time Complexity: O(n)
Space Complexity: O(1)

Method #3: Using the enumerate function

Use the enumerate function in this manner to get both the index and value of each entry in the list. Then, as before, we can use a list comprehension to generate a new list containing only the even indices and compute the span of even elements.

Step by step Algorithm:

  1. Initialize an empty list to store the indices of even elements.
  2. Iterate over the elements in the list using enumerate() function.
  3. Check if the element is even or not.
  4. If it’s even, appends its index to the even_indices list.
  5. Compute the difference between the last and first element of the even_indices list to get the res.
  6. Print the res.

Python3




test_list = [1, 3, 7, 4, 7, 2, 9, 1, 10, 11]
print("The original list is : " + str(test_list))
even_indices = [idx for idx, val in enumerate(test_list) if val % 2 == 0]
res = even_indices[-1] - even_indices[0]
print("Even elements span : " + str(res))


Output

The original list is : [1, 3, 7, 4, 7, 2, 9, 1, 10, 11]
Even elements span : 5

Time complexity: O(n), where n is the length of the input list. The algorithm iterates over the input list only once.
Auxiliary Space: O(m), where m is the number of even elements in the input list. The even_indices list will contain at most m elements.

Method #4: Using a loop

Step-by-step approach:

  • Initialize two variables, start and end, to None.
  • Loop through the elements of the list and check if the current element is even.
  • If the current element is even:
    a. If start is None, assign the index of the current element to start.
    b. Else, update end to the index of the current element.
  • After looping through all the elements, calculate the span by subtracting start from end and print the result.

Python3




# Python3 code to demonstrate working of
# Even elements span in list
# Using a loop
 
# initializing Matrix
test_list = [1, 3, 7, 4, 7, 2, 9, 1, 10, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing start and end
start = end = None
 
# looping through elements
for i in range(len(test_list)):
    if test_list[i] % 2 == 0# if current element is even
        if start is None:
            start = i
        else:
            end = i
 
# calculating span
res = end - start
 
# printing result
print("Even elements span : " + str(res))


Output

The original list is : [1, 3, 7, 4, 7, 2, 9, 1, 10, 11]
Even elements span : 5

Time complexity: O(n), where n is the length of the list. We are looping through the list only once.
Auxiliary space: O(1). We are using constant space for storing the start and end indices.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads