Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

New Array with Only Even Elements

Build a new list containing only even values in Python. Students filter without altering the source and preserve the order and repetitions of matching elements.

IntermediatePhase 4 - Level 3: TransformationExample 2 of 10
new-array-with-only-even-elements.py
1# Take array
2n = int(input("Enter array size: "))
3arr = []
4for i in range(n):
5 arr.append(int(input(f"Element {i+1}: ")))
6
7# Filter even elements
8even_arr = []
9for element in arr:
10 if element % 2 == 0:
11 even_arr.append(element)
12
13print(f"Original: {arr}")
14print(f"Even elements: {even_arr}")

Output

Enter array size: 5
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5
Element 5: 6
Original: [2, 3, 4, 5, 6]
Even elements: [2, 4, 6]

In this one

  • Filter into a new list
  • Preserve matching repetitions
  • Keep input order

How it works

  1. 1

    Create even_arr empty.

  2. 2

    Append each source value with even parity.

  3. 3

    Print both original and filtered lists.

Edge cases

Does zero qualify?

Does zero qualify?

Yes. It is even.

What if nothing qualifies?

What if nothing qualifies?

The new list remains empty.

What's going on

The loop tests each source value modulo two. A matching value is appended unchanged to even_arr.

Unlike a transformation, filtering may shorten the result. For 3, 2, 2, the new list is [2, 2], retaining both matches in input order. The original list still contains all three entries and is printed separately.

Does zero qualify? Yes. It is even.

What if nothing qualifies? The new list remains empty.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Are duplicates removed?

Pick an answer to check it.

Question 2

Does zero qualify?

Pick an answer to check it.

Questions people ask

Does zero qualify?

Yes. It is even.

What if nothing qualifies?

The new list remains empty.

Are duplicates removed?

No.