Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python program to print positive numbers in a list
Next article icon

Python program to print even numbers in a list

Last Updated : 23 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we’ll explore various methods to print even numbers from a list. The simplest approach is to use a loop for finding and printing the even numbers.

Using a Loop

We can simply use a loop (for loop) to find and print even numbers in a list.

Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  # Iterate through each element in the list for val in a:        # Checks if a number is divisible by 2 (i.e., even).     if val % 2 == 0:         print(val, end = " ") 

Output
2 4 6 8 10 

Let us explore different methods to print even numbers in a list.

Table of Content

  • Using List Comprehension
  • Using filter() Function
  • Using Bitwise AND Operator

Using List Comprehension

List comprehensions provide a more concise way to filter elements from a list.

Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  # Create a list of even numbers using list comprehension res = [val for val in a if val % 2 == 0] print(res) 

Output
[2, 4, 6, 8, 10] 

Explanation: The list comprehension [val for val in a if val % 2 == 0] creates a new list containing only even numbers from the original list.

Using filter() Function

Python also provides an inbuilt function filter() that can be used for filtering a list based on a condition.

Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  res = list(filter(lambda val: val % 2 == 0, a))  print(res) 

Output
[2, 4, 6, 8, 10] 

Explanation:

  • The filter() function filters elements of the list based on a provided function.
  • The lambda function lambda val: val % 2 == 0 acts as the condition to filter out even numbers.
  • We use list() to convert the filtered result back into a list.

Using Bitwise AND Operator

We can also use the bitwise AND operator (&) for checking if a number is even. This method works because even numbers always have their least significant bit set to 0 and if we take bitwise AND with 1 then the overall value would become 0.

Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  res = [val for val in a if val & 1 == 0]  print(res) 

Output
[2, 4, 6, 8, 10] 

Explanation:

  • The condition val & 1 == 0 evaluates to True for even numbers because the binary representation of even numbers ends in 0.
  • We use a list comprehension to collect all the even numbers from the list a and print the resulting list.


Next Article
Python program to print positive numbers in a list

S

Shivam_k
Improve
Article Tags :
  • DSA
  • Python
  • Python Programs
  • School Programming
  • Python list-programs
  • python-list
Practice Tags :
  • python
  • python-list

Similar Reads

  • Python program to print negative numbers in a list
    In Python, it’s often necessary to find and print negative numbers from a list. In this article we will explore various approches to print negative numbers in a list. The most basic method for printing negative numbers is to use a for loop to iterate through the list and check each element. [GFGTABS
    2 min read
  • Python program to print positive numbers in a list
    In this article, we will explore various methods to o print positive numbers in a list. The simplest way to do is by using for loop function. Using LoopThe most basic method for printing positive numbers is to use a for loop to iterate through the list and check each element. [GFGTABS] Python a = [-
    2 min read
  • Python Program to Count Even and Odd Numbers in a List
    In Python working with lists is a common task and one of the frequent operations is counting how many even and odd numbers are present in a given list. The collections.Counter method is the most efficient for large datasets, followed by the filter() and lambda approach for clean and compact code. Us
    4 min read
  • Python Program to Check if a Number is Odd or Even
    Even Numbers are exactly divisible by 2 and Odd Numbers are not exactly divisible by 2. We can use modulo operator (%) to check if the number is even or odd. For even numbers, the remainder when divided by 2 is 0, and for odd numbers, the remainder is 1. In this article, we will learn how to check i
    2 min read
  • Python Program to Print Largest Even and Largest Odd Number in a List
    Auxiliary Given a list. The task is to print the largest even and largest odd number in a list. Examples: Input: 1 3 5 8 6 10 Output: Largest even number is 10 Largest odd number is 5 Input: 123 234 236 694 809 Output: Largest odd number is 809 Largest even number is 694 The first approach uses two
    7 min read
  • Print odd numbers in a List - Python
    We are given a list and our task is to print all the odd numbers from it. This can be done using different methods like a simple loop, list comprehension, or the filter() function. For example, if the input is [1, 2, 3, 4, 5], the output will be [1, 3, 5]. Using LoopThe most basic way to print odd n
    2 min read
  • Python program to count positive and negative numbers in a list
    In this article, we will explore various methods to count positive and negative numbers in a list. The simplest way to do this is by using a loop. This method counts the positive and negative numbers in a list by iterating through each element using for loop. [GFGTABS] Python a = [10, -20, 30, -40,
    2 min read
  • Python program to print all positive numbers in a range
    In this article, we will explore various methods to print all positive numbers in a range. The simplest way to do this is by using a loop. Use a simple for loop to iterate through the given range and check if each number is greater than zero before printing it. [GFGTABS] Python start = -5 end = 3 #
    2 min read
  • Print all even numbers in a range - Python
    Our task is to print all even numbers within a given range. The simplest way to achieve this is by using a loop to iterate through the range and check each number for evenness. Let's explore some methods to do so. Using LoopWe can use a for loop with if conditional to check if a number is even. [GFG
    2 min read
  • Print Numbers in an Interval - Python
    In this article, we are going to learn how to print numbers within a given interval in Python using simple loops, list comprehensions, and step-based ranges. For Example: Input : i = 2, j = 5Output : 2 3 4 5 Input : i = 10, j = 20 , s = 2Output : 10 12 14 16 18 20 Let’s explore different methods to
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences