# 5 Best Ways to Print Elements on Even Positions in a Python Array

Rate this post

π‘ Problem Formulation: This article demonstrates how to print array elements that are located at even positions using Python. For an array `[3, 1, 4, 1, 5, 9, 2, 6]`, we aim to extract and print the elements at the 2nd, 4th, 6th, and so on positions, resulting in the desired output: `[1, 1, 9, 6]`.

## Method 1: Using a For Loop

This method iterates over the array using a for loop with a step of two, starting from index 1 which represents the second position in a zero-indexed array. The function is straightforward and is excellent for beginners to understand how indexing and loops work.

Here’s an example:

```arr = [3, 1, 4, 1, 5, 9, 2, 6]
for i in range(1, len(arr), 2):
print(arr[i])
```

Output:

```1
1
9
6
```

This code snippet creates a loop that starts at index 1 (second position), ends at the length of the array minus one (last position), and increments by two, thus only printing the elements at the even positions.

## Method 2: List Slicing

List slicing is a clean and efficient way to access specific portions of a list in Python. By using slicing, we can easily grab the even-positioned elements of an array without explicitly iterating over them.

Here’s an example:

```arr = [3, 1, 4, 1, 5, 9, 2, 6]
print(arr[1::2])
```

Output:

```[1, 1, 9, 6]
```

This snippet utilizes the syntax of list slicing. `arr[1::2]` starts the slice at the second element (index 1) of the array and proceeds to the end, selecting every second element.

## Method 3: Using List Comprehension

Python’s list comprehensions are a concise way to create lists. They can be used to derive a new list by evaluating an expression over an iterable, selectively including elements.

Here’s an example:

```arr = [3, 1, 4, 1, 5, 9, 2, 6]
even_pos_elements = [arr[i] for i in range(1, len(arr), 2)]
print(even_pos_elements)
```

Output:

```[1, 1, 9, 6]
```

This code utilizes list comprehension to iterate over the array and create a new list that contains the array’s elements at the even positions (or odd indexes).

## Method 4: Using enumerate and Filtering

The enumerate function in Python adds a counter to an iterable. In conjunction with filtering, this can be used to identify and print elements at even positions.

Here’s an example:

```arr = [3, 1, 4, 1, 5, 9, 2, 6]
for index, value in enumerate(arr):
if index % 2 != 0:
print(value)
```

Output:

```1
1
9
6
```

By combining `enumerate` with a simple conditional within a for loop, this code checks if the index is not divisible by two (meaning it’s an odd index which corresponds to an even position) and prints the corresponding element.

## Bonus One-Liner Method 5: Using filter and lambda

Python’s filter function allows you to process an iterable and extract elements that meet a condition. Combined with a lambda function, this makes for a terse one-liner that gets the job done.

Here’s an example:

```arr = [3, 1, 4, 1, 5, 9, 2, 6]
print(list(filter(lambda x: arr.index(x) % 2 != 0, arr)))
```

Output:

```[1, 1, 9, 6]
```

This snippet uses `filter` along with a `lambda` function to iterate over the array and select elements whose index value is odd. Note that using `arr.index(x)` is not very efficient for large arrays as it searches the array every time itβs called.

## Summary/Discussion

• Method 1: For Loop. Suitable for teaching purposes. The iteration approach might be slower for very large arrays.
• Method 2: List Slicing. Elegant and Pythonic. Offers the best performance for most cases but may be less readable for beginners.
• Method 3: List Comprehension. Flexible and Pythonic. Balances readability with efficiency. Good for intermediate Python users.
• Method 4: Enumerate and Filtering. Explicit and easy to understand what each iteration is doing. Might be a bit slower due to the conditional check every loop.
• Method 5: filter and lambda. Very concise. However, it’s not recommended for performance-sensitive tasks due to `arr.index` inherent inefficiency.