Open In App

Python - List consisting of all the alternate elements

Last Updated : 11 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

We are given a list consisting we need to get all alternate elements of that list. For example, we are given a list a = [1, 2, 3, 4, 5, 6, 7, 8] we need to get all alternate elements of that list so that output should be [1, 3, 5, 7].

Using List Slicing

List slicing allows us to select every alternate element by specifying a step of 2 in the slice notation. This creates a new list with elements at even or odd indices depending on starting index.

Python
a = [1, 2, 3, 4, 5, 6, 7, 8]

# Use slicing to get every second element starting from index 0
res = a[::2]
print(res)

Output
[1, 3, 5, 7]

Explanation:

  • Slice notation a[::2] selects every second element from list a starting from index 0.
  • Result is a new list res containing alternate elements

Using List Comprehension

List comprehension allows us to iterate through list and select every alternate element by checking if index is even results in a new list containing filtered elements.

Python
a = [1, 2, 3, 4, 5, 6, 7, 8]

# List comprehension to pick alternate elements
res = [a[i] for i in range(len(a)) if i % 2 == 0]
print(res)

Output
[1, 3, 5, 7]

Explanation:

  • List comprehension iterates through indices of list a checking if index is even (i % 2 == 0).
  • For each even index corresponding element is added to list res resulting in alternate elements.

Using filter() and lambda

filter() function with a lambda function checks if index of each element is even using enumerate() to access both index and value result is an iterator containing only elements at even indices which can be converted to a list.

Python
a = [1, 2, 3, 4, 5, 6, 7, 8]

# Use filter with lambda to select alternate elements
res = list(filter(lambda x: a.index(x) % 2 == 0, a))
print(res)

Output
[1, 3, 5, 7]

Explanation:

  • filter() function uses a lambda to check if the index of each element in the list a is even by using a.index(x) % 2 == 0.
  • filter() returns an iterator which is converted to a list res containing the alternate elements.

Practice Tags :

Similar Reads