Print alternate elements of list Python using slicing

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.




# 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))
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.




# 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))
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.




# 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))
Output: The original list : [1, 4, 6, 7, 9, 3, 5] The alternate element list is : [4, 7, 3]

Print alternate elements of list Python using slicing




Article Tags :
Python
Python Programs
Python list-programs

Python – Alternate List elements

Given 2 lists, print element in zig-zag manner, i.e print similar indices of lists and then proceed to next.

Input : test_list1 = [5, 3, 1], test_list2 = [6, 4, 2]
Output : [5, 6, 3, 4, 1, 2, 4]
Explanation : 5 and 6, as in 0th index are printed first, then 3 and 4 on 1st index, and so on.

Input : test_list1 = [5, 3, 1, 9], test_list2 = [6, 4, 2, 10]
Output : [5, 6, 3, 4, 1, 2, 4, 9, 10]
Explanation : 5 and 6, as in 0th index are printed first, then 3 and 4 on 1st index, and so on.

Method #1 : Using loop

This is one of the ways in which this task can be performed. In this we plainly perform iteration and append the similar index elements one after another in result list.






# Python3 code to demonstrate working of
# Alternate List elements
# Using loop
# initializing lists
test_list1 = [5, 3, 1, 4, 7]
test_list2 = [6, 4, 2, 5, 1]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# Using loop to print elements in criss cross manner
res = []
for idx in range(0, len(test_list1)):
res.append(test_list1[idx])
res.append(test_list2[idx])
# printing result
print("The zig-zag printing of elements : " + str(res))
Output The original list 1 : [5, 3, 1, 4, 7] The original list 2 : [6, 4, 2, 5, 1] The zig-zag printing of elements : [5, 6, 3, 4, 1, 2, 4, 5, 7, 1]

Method #2 : Using zip() + loop

The combination of above functions can be used to solve this problem. In this, we pair each element with similar index using zip() and then each element is fed into result list using loop.




# Python3 code to demonstrate working of
# Alternate List elements
# Using zip() + loop
# initializing lists
test_list1 = [5, 3, 1, 4, 7]
test_list2 = [6, 4, 2, 5, 1]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# Using zip() to perform pairing and loop to
# get elements into result list
res = []
for ele1, ele2 in zip(test_list1, test_list2):
res.append(ele1)
res.append(ele2)
# printing result
print("The zig-zag printing of elements : " + str(res))
Output The original list 1 : [5, 3, 1, 4, 7] The original list 2 : [6, 4, 2, 5, 1] The zig-zag printing of elements : [5, 6, 3, 4, 1, 2, 4, 5, 7, 1]

Print alternate elements of list Python using slicing




Article Tags :
Python
Python Programs
Python list-programs

List consisting of all the alternate elements in Python

PythonServer Side ProgrammingProgramming

In this article, we are going to learn how to get alternate elements from the list. We'll see two different ways to solve the problem.

Follow the below steps to solve the problem in one way.

  • Initialize the list.
  • 3Iterate over the list and store all the elements from the odd index.
  • Print the result.

3 ways to create a list of alternate elements in Python

Print alternate elements of list Python using slicing

“how to print alternate numbers in python” Code Answer


how to print alternate numbers in python
python by bokaif
Print alternate elements of list Python using slicing
on May 17 2021 Comment
1
Add a Grepper Answer

  • how to sum only the odd values in python
  • how to sum only the even values in python
  • program to find even numbers in python
  • fastest way to check odd or even in python
  • sum of any numbers in python
  • list comprehension odd numbers python
  • wap in python to check a number is odd or even
  • python for loop even numbers
  • print 1to 10 number without using loop in python
  • while loop odd numbers python
  • print even numbers in python
  • how to make a python program on odd and even
  • python sum only numbers
  • how to sum 2 no.s in python
  • Write Python programs to print numbers from 1 to 10000 while loops
  • python how do index all odd numbers in a list
  • program to find sum till n natural numbers in python

  • how to print alternate numbers in python
  • how to get alternate numbers in python
  • print alternate numbers in python

How do you use alternate elements in a list Python?

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

  1. Method #1 : Using list comprehension.
  2. Method #2 : Using enumerate()
  3. Method #3 : Using Slice notation.
  4. To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

How do I add alternate numbers to a list in Python?

Use the percentage operator to separate the numbers at Even and Odd positions. And then add the elements to the respective position of a new empty list. Finally giving a list which shows sum of elements at odd position and sum of elements at even position.

How do you make a list with N elements in Python?

To create a list of n placeholder elements, multiply the list of a single placeholder element with n . For example, use [None] * 5 to create a list [None, None, None, None, None] with five elements None . You can then overwrite some elements with index assignments.

Python program to alternate elements operation on tuple

Here, we have a tuple consisting of multiple elements. We need to create a python program that will perform alternate elements operations on tuples.
Submitted by Shivang Yadav, on September 30, 2021

Python programming language allows its users to work on data structures and manipulate them based on some given condition. In this program, we have a tuple consisting of multiple elements and perform the task of alternate element operation on tuples the operation is chain sum.

Input: (5, 1, 7, 3, 9) Output: Alternate chain sum = 1 + 3 = 4 Alternate chain sum = 5 + 7 + 9 = 21

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Python: Create a list taking alternate elements from a given list

Last update on December 01 2020 16:54:27 (UTC/GMT +8 hours)