Get only the Even or Odd index Elements of a Python List

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Get only the even or odd index elements of a Python List
  2. Get only the even or odd index elements of a Python List using a for loop
  3. Get only the even or odd index elements of a List using islice()

# Get only the even or odd index elements of a Python List

Use list slicing to split the even/odd index elements into two lists, e.g. even = my_list[::2] and odd = my_list[1::2].

The first list slice will only contain the elements with an even index and the second the elements with an odd index.

main.py
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # โœ… get all elements with an even index in the list even = my_list[::2] print(even) # ๐Ÿ‘‰๏ธ [0, 2, 4, 6, 8, 10] # --------------------------------------------- # โœ… get all elements with an odd index in the list odd = my_list[1::2] print(odd) # ๐Ÿ‘‰๏ธ [1, 3, 5, 7, 9] # --------------------------------------------- # โœ… get every second element of the list (starting with the second element) n = 2 every_second = my_list[n - 1::n] print(every_second) # ๐Ÿ‘‰๏ธ [1, 3, 5, 7, 9] # --------------------------------------------- # โœ… get every second element of the list (starting at index 2) every_second = my_list[2::2] print(every_second) # ๐Ÿ‘‰๏ธ [2, 4, 6, 8, 10]

get only even or odd index elements in list

The code for this article is available on GitHub

We used list slicing to get only the even index elements of a list.

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

The slice my_list[::2] gets the even index elements of the original list.

main.py
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even = my_list[::2] print(even) # ๐Ÿ‘‰๏ธ [0, 2, 4, 6, 8, 10]

The start and end indexes are omitted, so the slice starts at index 0 and goes to the end of the list.

The step value is set to 2, so the slice selects every second element, starting at index 0.

You can use the same approach to get only the elements with an odd index of a list.

main.py
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # โœ… Get all elements with odd index in the list odd = my_list[1::2] print(odd) # ๐Ÿ‘‰๏ธ [1, 3, 5, 7, 9]
The code for this article is available on GitHub

Instead of starting at index 0, the slice starts at index 1 and selects every second element.

The stop index is omitted, so the slice selects the odd elements of the entire list.

If you need to get every Nth element of a list, specify a value for the start index.

main.py
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # โœ… get every second element of the list (starting at second element) n = 2 every_second = my_list[n - 1::n] print(every_second) # ๐Ÿ‘‰๏ธ [1, 3, 5, 7, 9]

The example selects every second element of the list starting at the second element.

If you need to get the elements with an even index of a list, starting at a specific index, set the value for the start index.

main.py
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] every_second = my_list[2::2] print(every_second) # ๐Ÿ‘‰๏ธ [2, 4, 6, 8, 10]

The new list consists of the even elements of the original list starting at index 2 (the third element).

Alternatively, you can use a for loop.

# Get only the even or odd index elements of a Python List using a for loop

This is a three-step process:

  1. Declare even and odd variables and set them to empty lists.
  2. Iterate over the original list.
  3. Push the even index elements into the even list and the odd index elements into the odd list.
main.py
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even = [] odd = [] for index, item in enumerate(my_list): if index % 2 == 0: even.append(item) else: odd.append(item) print(even) # ๐Ÿ‘‰๏ธ [0, 2, 4, 6, 8, 10] print(odd) # ๐Ÿ‘‰๏ธ [1, 3, 5, 7, 9]

get only the even or odd index elements using for loop

The code for this article is available on GitHub

The enumerate() function takes an iterable and returns an enumerate object containing tuples where the first element is the index, and the second is the item.

On each iteration, we use the modulo operator to check if the current index is even or odd.

The modulo (%) operator returns the remainder from the division of the first value by the second.

main.py
print(10 % 2) # ๐Ÿ‘‰๏ธ 0 print(10 % 4) # ๐Ÿ‘‰๏ธ 2

The last step is to append the even indexes to the even list and the odd indexes to the odd list.

# Get only the even or odd index elements of a List using islice()

You can also use the islice class from the itertools module to get only the even or odd number elements of a list.

main.py
from itertools import islice my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even = list(islice(my_list, 0, len(my_list), 2)) print(even) # ๐Ÿ‘‰๏ธ [0, 2, 4, 6, 8, 10] odd = list(islice(my_list, 1, len(my_list), 2)) print(odd) # ๐Ÿ‘‰๏ธ [1, 3, 5, 7, 9]

get only even or odd index elements using islice

The code for this article is available on GitHub

The itertools.islice() class takes an iterator and optional start, stop and step values.

The second argument we passed to the islice() class is the index of the first element to be included in the slice.

The third argument is the index of the first element to be excluded from the slice.

The fourth argument is the step.

We used a start index of 0 and a step of 2 to get the even index elements from the list.

To get the odd number elements, we used a start index of 1 and a step value of 2.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev