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 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)) ` |

*chevron_right*

*filter_none*

**Output :**

The original list is : [43, 9, 6, 72, 8, 11] The first even element in list is : 6

**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 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)) ` |

*chevron_right*

*filter_none*

**Output :**

The original list is : [3, 7, 8, 9, 10, 15] The first even element in list is : 8

## Recommended Posts:

- Python | Sort the values of first list using second list
- Python | Replace elements in second list with index of same element in first list
- Python | Subtract two list elements if element in first list is greater
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Python program to count Even and Odd numbers in a List
- Python program to print even numbers in a list
- Python | Filter even values from a list
- Python - Get Indices of Even Elements from list
- Python | Even Front digits Test in List
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python program to interchange first and last elements in a list
- Python | First Non-Empty String in list
- Python - Remove first element of list
- Python | Get first element with maximum value in list of tuples
- Python | Get first and last elements of a list
- Python | Shift last element to first position in list
- Python | Combine two lists by maintaining duplicates in first list

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.