Python get Even Integers from Python List

We can return only even integers from lists in python using for loop, while loop, List comprehension, and lambda expression. Besides these, you can also convert our list to the numpy array and use the NumPy filter.

An even number is a number that is divisible by 2 and we will use this on pretty much all examples to get even numbers from the list.

1. Quick Examples of Returning Even Integers from List

Following are quick examples of returning only even numbers from the list.


# Quick Examples of Returning Even Integers from List

# Consider list of marks
student_marks=[90,81,78,93,100]

# Using for loop
evenlist=[]
for i in student_marks:
	if (i % 2 == 0):
		evenlist.append(i)
print(evenlist)

# Using while loop
evenlist=[]
i=0
while(i < len(student_marks)):
    # checking condition
    if (student_marks[i] % 2 == 0):
        evenlist.append(student_marks[i])
    i += 1
print(evenlist)

# Using enumerate
evenlist=[]
for a, i in enumerate(student_marks):
    if (i % 2 == 0):
        evenlist.append(i)
print(evenlist)

# Using List Comprehension
print([i for i in student_marks if (i % 2 == 0)])

# Using lambda expression
print(list(filter(lambda i: (i % 2 == 0), student_marks)))

# Using numpy.array()
import numpy
print(list(numpy.array(student_marks)[numpy.array(student_marks) % 2 == 0]))

2. Using for loop

To get even numbers from the Python List you can use the for loop to iterate each element in a list and check if it is an even number, if it then add it to a new list. We are checking if the iterator is divisible by 2 or not to find the even number

Let’s have a list with 5 integers and return the list with even integers.


# Consider list of marks
student_marks=[90,81,78,93,100]

# Using for loop
evenlist=[]
for i in student_marks:
	if (i % 2 == 0):
		evenlist.append(i)
print(evenlist)

# Output:
# [90, 78, 100]

3. Using while loop

Similarly, you can also use the while loop to iterate each element in a list and even numbers to the new list. With while loop, after each iteration, we need to increment the iterator by 1.

We are checking if the iterator is divisible by 2 or not. If yes, we will store it in a list (Holds even integers). Finally we will display the list. Let’s have a list with 5 integers and return the list with even integers.


# Consider list of marks
student_marks=[90,81,78,93,100]

# Using while loop
evenlist=[]
i=0
while(i < len(student_marks)):
    # checking condition
    if (student_marks[i] % 2 == 0):
        evenlist.append(student_marks[i])
    i += 1
print(evenlist)

# Output:
# [90, 78, 100]

4. Using for loop with enumerate()

The enumerate() is used with for loop like the above approach. enumerate will also return the element index. So we don’t need that index. We will append only even integers into new list.

Let’s have a list with 5 integers and return the list with even integers using for loop with enumerate().


# Consider list of marks
student_marks=[90,81,78,93,100]

# Using enumerate
evenlist=[]
for a, i in enumerate(student_marks):
    if (i % 2 == 0):
        evenlist.append(i)
print(evenlist)

# Output:
# [90, 78, 100]

5. Using List Comprehension

List comprehension uses for loop to iterate each element in list and check if the iterated value is divisible by 2 using if condition. It will return the values that matches the condition. Let’s see the syntax of using List Comprehension.


# Here, list1 is the input list.
[iterator for iterator in list1 if (iterator % 2 == 0)]

Let’s have a list with 5 integers and return the list with even integers using List Comprehension.


# Consider list of marks
student_marks=[90,81,78,93,100]

# Using List Comprehension
print([i for i in student_marks if (i % 2 == 0)])

# Output:
# [90, 78, 100]

6. Using filter with lambda expression

filter() method is used to filter the iterable (Here it is list) based on the condition specified. Here, we will pass the lambda expression as the first parameter that checks if value is divisible by 2 or not and second parameter is the input list.

Let’s see the syntax of using filter() with the lambda expression.


# Here, list1 is the input list.
list(filter(lambda x: (x % 2 == 0), list1))

Let’s have a list with 5 integers and return the list with even integers using lambda expression


# Consider list of marks
student_marks=[90,81,78,93,100]

# Using lambda expression
print(list(filter(lambda i: (i % 2 == 0), student_marks)))

# Output:
# [90, 78, 100]

7. Using numpy

We can directly provide the condition with the numpy array to return even numbers. So we need to convert our list to numpy array using numpy.array(). Inside this we will filter the even numbers – [numpy.array(list1) % 2 == 0].

Finally, we need to convert this numpy array back to list. Let’s see the syntax of using numpy.array()


# Here, list1 is the input list.
list(numpy.array(list1)[numpy.array(list1) % 2 == 0])

Let’s have a list with 5 integers and return the list with even integers.


import numpy
# Consider list of marks
student_marks=[90,81,78,93,100]

# Using numpy.array()
print(list(numpy.array(student_marks)[numpy.array(student_marks) % 2 == 0]))

# Output:
# [90, 78, 100]

8. Conclusion

By using for loop and while loop, we returned only even integers from the list. List comprehension and filter() method with lambda expression can also be used to filter only even integers. By converting our list to the numpy array, we can filter even integers directly specifying the condition inside this numpy array.

Leave a Reply