Get Every Other Element in List in Python

A list is one of the more versatile data types among the others provided in Python. As we know lists can store multiple items in one single variable, there is often a need to get only a handful of items from a given list.

Get Every Other Element in List in Python

This tutorial focuses on and demonstrates the different ways available to get every other element in a list in python.

There are numerous ways to tackle this problem, ranging from simple list slicing to a lambda function, all of which have been thoroughly explained in this article below.

Using List Slicing

List slicing is a common practice and is utilized to access the elements of a list in a specified range.
List slicing is able to return a new, more specific list from the original list by taking in the start, end, and step values specified by the user. All three are separated by a colon [:] sign.

The parameters mentioned above have the following explanations:

  • start: The beginning point of the range that the user defines.
  • end: The end point of the range that the user defines.
  • step: the step count that the user can define.

The following code uses list slicing to get every other element in a list in Python.

Output:

[10,30,50,70,90]

Using the list.append() function along with the range() function

The list.append() method, as the name suggests, is utilized to append items in a given list.
The range() function is used to specify a range that the function is to be applied upon. This function has three parameters, which are start, stop, and step. All of these three are separated by a comma [,] sign.

  • start: It marks the beginning of the range.
  • stop: It marks the end of the range.
  • step: It marks the step value that is to be taken.

The following code uses the list.append() function along with the range() function to get every other element in a list in Python.

Output:

[20,40,60,80,100]

Here is article on how to increment for loop by 2 in Python.

Using list comprehension and the range() function with conditionals

List comprehension is capable of creating a list with reference to an already existing list. It also reduces the chunk of code and makes the code more compact.
Here, we utilize the range() function with conditionals rather than the convention start, stop, and step parameters.

The following code uses list comprehension and the range() function with conditionals to get every other element in a list in Python.

Output:

[10,30,50,70,90]

Using list comprehension and the range() function with step

List comprehension, as we know, helps us avoid the generic for loop and makes it possible to finish the code in a single line.
Here, we utilize the range() function with the conventional start, stop, and step to implement the task at hand.

The following code uses list comprehension and the range() function with step to get every other element in a list in Python.

Output:

[10,30,50,70,90]

Using the enumerate() function along with list comprehension

The enumerate() function is a handy addition to a user’s Python code as this purpose serves the function of assisting the user by adding a counter to any given iterable and returning it as an enumerating object.

List comprehension, when used along with the enumerate() function, is capable of implementing the task of getting every other element in a list in Python.

The following code uses the enumerate() function along with list comprehension to get every other element in a list in Python.

Output:

[10, 30, 50, 70, 90]

Using a lambda function

A lambda function is an anonymous function that is capable of holding a single expression while taking in n number of arguments.

The lambda function can implement the task of getting every other element in a list in Python when utilized with the range() function and specifying conditional.

This approach also makes use of an additional filter() function that is utilized to place the elements of a given sequence under a conditional and tests whether they are true or not. On the basis of this, it filters the specified sequence.

The following code uses the lambda function to get every other element in a list in Python.

Output:

[20, 40, 60, 80, 100]

Conclusion.

This tutorial discusses several methods that successfully carry out the task of getting every other element in a list in Python. Some methods can be quite complex for beginners to understand but all of the methods provide accurate answers. The programmer can choose one out of the lot depending on the results they are seeking.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *