Python | List consisting of all the alternate elements

Some of the list operations are quite general and having shorthands without needing to formulate a multiline code is always required. Wanting to construct the list consisting of all the alternate elements of the original list is a problem that one developer faces in day-day applications.

Let’s discuss certain ways to print all the alternate elements of the given list.

Method #1 : Using list comprehension



Shorthand to the naive method, list comprehension provides a faster way to perform this particular task. In this method, all the indices which are not multiple of 2, hence odd are inserted in the new list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate 
# to construct alternate element list
# using list comprehension 
  
# initializing list 
test_list = [1, 4, 6, 7, 9, 3, 5]
  
# printing original list 
print ("The original list : " + str(test_list))
  
# using list comprehension
# to construct alternate element list
res = [test_list[i] for i in range(len(test_list)) if i % 2 != 0]
  
# printing result 
print ("The alternate element list is : " + str(res))

chevron_right


Output:

The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element list is : [4, 7, 3]

 
Method #2 : Using enumerate()
This is just a variation to the list comprehension method but does the similar internal working as list comprehension but uses different variables to keep track of index along with its value.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate 
# to construct alternate element list
# using enumerate() 
  
# initializing list 
test_list = [1, 4, 6, 7, 9, 3, 5]
  
# printing original list 
print ("The original list : " + str(test_list))
  
# using enumerate()
# to construct alternate element list
res = [i for j, i in enumerate(test_list) if j % 2 != 0]
  
# printing result 
print ("The alternate element list is : " + str(res))

chevron_right


Output:

The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element list is : [4, 7, 3]

 
 Method #3 : Using Slice notation
This the most pythonic and elegant way to perform this particular task and enhances to the full power of python. We can use the skip utility provided by slice to get the alternate elements from start to end in the list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate 
# to construct alternate element list
# using Slice notation
  
# initializing list 
test_list = [1, 4, 6, 7, 9, 3, 5]
  
# printing original list 
print ("The original list : " + str(test_list))
  
# using Slice notation
# to construct alternate element list
res = test_list[1::2]
  
# printing result 
print ("The alternate element list is : " + str(res))

chevron_right


Output:

The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element list is : [4, 7, 3]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.