List of even integers python hackerrank solution

PythonServer Side ProgrammingProgramming

In this article, we will learn about the solution and approach to solve the given problem statement.

Problem statement

Given a list iterable, we need to print all the even numbers in the list.

Here we will be discussing three approaches for the given problem statement.

Approach 1 − Using enhanced for loop

Example

list1 = [11,23,45,23,64,22,11,24] # iteration for num in list1:    # check    if num % 2 == 0:       print(num, end = " ")

Output

64 22 24

Approach 2 − Using filter & lambda function

Example

list1 = [11,23,45,23,64,22,11,24] # lambda exp. even_no = list(filter(lambda x: (x % 2 == 0), list1)) print("Even numbers in the list: ", even_no)

Output

Even numbers : [64, 22, 24]

Approach 3 − Using list comprehension

Example

list1 = [11,23,45,23,64,22,11,24] #list comprehension even_nos = [num for num in list1 if num % 2 == 0] print("Even numbers : ", even_nos)

Output

Even numbers : [64, 22, 24]

Conclusion

In this article, we learned about the approach to print even numbers in the input list.

List of even integers python hackerrank solution

Updated on 04-Jul-2020 12:56:14

Last update on August 19 2022 21:51:39 (UTC/GMT +8 hours)

Write a Python program to print the even numbers from a given list.

List of even integers python hackerrank solution

Sample Solution:-

Python Code:

def is_even_num(l): enum = [] for n in l: if n % 2 == 0: enum.append(n) return enum print(is_even_num([1, 2, 3, 4, 5, 6, 7, 8, 9]))

Sample Output:

[2, 4, 6, 8]

Flowchart:

List of even integers python hackerrank solution

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python function that takes a number as a parameter and check the number is prime or not.
Next: Write a Python function to check whether a number is perfect or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Check the Type of an Object:

>>> def check_type(number): ... if type(number) == int: ... print('do something with an int') ... if isinstance(number, (int, float)): ... print('do something with an int or float') ... >>> check_type(5) do something with an int do something with an int or float >>> check_type(4.2) do something with an int or float

  • Exercises: Weekly Top 12 Most Popular Topics
  • Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • C Programming Exercises, Practice, Solution : For Loop
  • Python Exercises, Practice, Solution
  • Python Data Type: List - Exercises, Practice, Solution
  • C++ Basic: Exercises, Practice, Solution
  • SQL Exercises, Practice, Solution - exercises on Employee Database
  • SQL Exercises, Practice, Solution - exercises on Movie Database
  • SQL Exercises, Practice, Solution - exercises on Soccer Database
  • C Programming Exercises, Practice, Solution : Recursion

Given a list of numbers, write a Python program to print all even numbers in the given list.

Example: 



Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] Input: list2 = [12, 14, 95, 3] Output: [12, 14]

Method 1: Using for loop

Iterate each element in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number. 

list1 = [10, 21, 4, 45, 66, 93]

Output: 

10, 4, 66

Method 2: Using while loop 

list1 = [10, 24, 4, 45, 66, 93]

        print(list1[num], end=" ")

Output: 

10, 4, 66

Method 3: Using list comprehension 

list1 = [10, 21, 4, 45, 66, 93]

even_nos = [num for num in list1 if num % 2 == 0]

print("Even numbers in the list: ", even_nos)

Output: 

Even numbers in the list: [10, 4, 66]

Method 4: Using lambda expressions 

list1 = [10, 21, 4, 45, 66, 93, 11]

even_nos = list(filter(lambda x: (x % 2 == 0), list1))

print("Even numbers in the list: ", even_nos)

Output: 

Even numbers in the list: [10, 4, 66]

Method 5: Using Recursion

def evennumbers(list, n=0):

list1 = [10, 21, 4, 45, 66, 93]

print("Even numbers in the list:", end=" ")

Output Even numbers in the list: 10 4 66

list1 = [2, 7, 5, 64, 14]

for a,i in enumerate(list1):


This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.

Question: Write a program which first defines a function evens(n). The function should take an integer as the argument and return a list of n even integers starting with 2 using the range function which takes a third argument that is the "step". Rewrite the function using the third argument in the range.

What I have so far:

def evens(n): evensLst = [] for i in range(1,n+1): evensLst.append(2*i) return evensLst for i in range(1,n+1): evensLst.append(3*i) return evensLst n = raw_input("Enter an integer: ") print "Evens: ", evens(n)