How to find the first even and odd occurring numbers from a list in Python?
Tags: howto, python, list, datastructures
Published: 2023-12-26 17:47:25

We usually find problems where they tell us to separate the odd and even numbers from the list. But this question is unique as you must find the _first-ever even number_ you encounter and the _first-ever odd number_ you encounter inside the list.

Here are the example inputs and outputs.

**Example Input 1:**

33,44,55,77,88

**Example Output 1:**

[33, 44, 55, 77, 88]
First even: 44 and first odd: 33

**Example Input 2:**

12,44,86,32,66,92,100

**Example Output 2:**

[12, 44, 86, 32, 66, 92, 100]
First even: 12 and first odd: Not found


![Example of the output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3nja15kn683cgv1j6bh.gif)

---

## **Let’s set up some steps for this problem:**

1. Implementing user input as a list.
2. Setting up variables.
3. Searching the odd and even numbers.

---
To solve this problem, we must take input from the user and convert it into a list. There are various ways of making a list by taking input from the user, but we usually use some techniques that could be more neat. The messiest way to make a list would be — setting up the number of elements for a list. The to type in each element while pressing enter after those elements. This is a messy way of taking input from the user and making it a list.


![The messiest way to make a list from user inputs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ecaq3rr4tr7e2xe1wbre.gif)

<img width="200%" style="width:200%" src="https://media.giphy.com/media/pwnpqOmGHfBdQPWU3e/giphy.gif">

## **Step 1:**
Let’s code an effective way of handling input as a list.

```
my_list = input().split(",")
for n in range(0, len(my_list)):
  my_list[n] = int(my_list[n])

print(my_list) 
#I'm printing the list to 
#show you how it works compared to the above messy example.

```


![Good List](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3iyl22ll31jahmj4uh0y.gif)

Now, will you look at that!

<img width="200%" style="width:200%" src="https://media.giphy.com/media/oNKLBehxbnoqY/giphy.gif">

## **Step 2:**
We would then set 2 variables named `comment` and `comment_2` and set them as “Not Found”. These comment variables will also act like a medium of output if there are no even or odd numbers in the list.

```
comment = "Not found"
comment_2 = "Not found"

#I have taken two comment variables, one for even and one for odd.
```
## **Step 3:**
Now, we will search the even numbers and the odd numbers from the list with a simple modulus “%” operator in Python.

<img width="200%" style="width:200%" src="https://media.giphy.com/media/5zoxhCaYbdVHoJkmpf/giphy.gif">

We will put the list through a for loop, and if I see that my element is odd or even, I will swap the value of `comment` or `comment_2` with the item I have just found from the list.

<img width="200%" style="width:200%" src="https://media.giphy.com/media/Xf8D9Qf8OCKnMvNnru/giphy.gif">

```
# Check for even numbers

for items in my_list:
  if items % 2 == 0:
    comment = items   #Notice that I have assigned the variable to items
    break

```
We will repeat the same process for odd numbers, and finally, we are done 🎉🎉💥💥

<img width="200%" style="width:200%" src="https://media.giphy.com/media/7yORCExjS87Jk10xSU/giphy.gif">

## **Final Code:**

```
my_list = input().split(",")
for n in range(0, len(my_list)):
  my_list[n] = int(my_list[n])

#This code above solves the problem. 
#We have taken the list as an input in a neat way.

comment = "Not found"
comment_2 = "Not found"


#I have taken two comment variables, 
#one for even and one for odd. 

# Next, we check for even numbers

for items in my_list:
  if items % 2 == 0:
    comment = items   #Notice that I have assigned the variable to items
    break


#checking for odd numbers and repeating the whole process

for items_2 in my_list:
  if items_2 % 2 != 0:
    comment_2 = items_2
    break
    
print(my_list)
print("First even: " + str(comment), "and first odd: " + str(comment_2))
```
## **You can find the full code from my GitHub from [here](https://github.com/YbArzoo/Finding-the-first-ever-occurring-even-and-odd-numbers-from-a-list-in-Python).**

---
## **Note for the readers.**

I’m genuinely grateful for all the support you all kind souls have shown in my [previous article](https://dev.to/ybarzoo/how-to-make-a-fibonacci-series-with-a-function-in-python-26e2). With all the support you guys have shown me, I couldn’t stop writing another Python Programming article for you guys 💜!

Could you all react to show your support and keep my enthusiasm going? This will ignite my passion, and I will continue giving my best! 🔥

_FYI — You can all find me on [Twitter (X)](https://twitter.com/ArzooYb) and [LinkedIn](https://www.linkedin.com/in/yb-arzoo/)._