Python | Add element at alternate position in list
Last Updated :
30 Mar, 2023
Sometimes, while working with Python list, we can have a problem in which we need to add elements in list alternatively i.e at even positions and reorder the list accordingly. This has a potential application in many domains such as day-day programming and competitive programming. Let's discuss certain way in which this problem can be solved.
Method : Using join() + list() This method can be used to solve this problem in one line. In this, we just join all the elements alternatively with target element and then convert back to list using list().
Python3
# Python3 code to demonstrate working of
# Add element at alternate position in list
# using join() + list()
# initialize list
test_list = ['a', 'b', 'c', 'd', 'e', 'f']
# printing original list
print("The original list is : " + str(test_list))
# initialize ele
ele = '#'
# Add element at alternate position in list
# using join() + list()
res = list(ele.join(test_list))
# printing result
print("List after alternate addition : " + str(res))
OutputThe original list is : ['a', 'b', 'c', 'd', 'e', 'f']
List after alternate addition : ['a', '#', 'b', '#', 'c', '#', 'd', '#', 'e', '#', 'f']
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Using the itertools.chain() function: Use the itertools.chain() function to iterate through the list and add the target element at every even index. Time complexity of this approach would be O(n) and Auxiliary space would be O(1).
Python3
import itertools
# initialize list
test_list = ['a', 'b', 'c', 'd', 'e', 'f']
# printing original list
print("The original list is : " + str(test_list))
# initialize ele
ele = '#'
# Add element at alternate position in list
# using itertools.chain()
res = list(itertools.chain.from_iterable(zip(test_list, [ele]*(len(test_list)-1))))
# printing result
print("List after alternate addition : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list is : ['a', 'b', 'c', 'd', 'e', 'f']
List after alternate addition : ['a', '#', 'b', '#', 'c', '#', 'd', '#', 'e', '#']
Time Complexity: O(n), where n is the length of test_list.
Auxiliary Space: O(n), where n is the number of elements in res list.
Similar Reads
Python - Repeat a element in a List In Python, we often need to add duplicate values to a list, whether for creating repeated patterns, frequency counting, or handling utility cases. In this article, there are various methods to Repeat an element in a List. * operator allows us to repeat an entire list or a list element multiple times
3 min read
Python - Print list after removing element at given index In this article, we will explore different ways to remove an element from a list at a given index and print the updated list, offering multiple approaches for achieving this.Using pop()pop() method removes the element at a specified index and returns it.Pythonli = [10, 20, 30, 40, 50] index = 2 li.p
2 min read
How to add Elements to a List in Python In Python, lists are dynamic which means that they allow further adding elements unlike many other languages. In this article, we are going to explore different methods to add elements in a list. For example, let's add an element in the list using append() method:Pythona = [1, 2, 3] a.append(4) prin
2 min read
Prepend Elements to Lists in Python In Python, prepending means adding elements to the beginning of a list. We can think of it as putting something at the front of a queue. In this article, we will explore various methods to prepend elements to lists in Python. The insert() method to add an element at a specific position in a list. To
2 min read
Python | Insert Nth element to Kth element in other list Sometimes, while working with Python list, there can be a problem in which we need to perform the inter-list shifts of elements. Having a solution to this problem is always very useful. Letâs discuss the certain way in which this task can be performed. Method 1: Using pop() + insert() + index() This
9 min read
Perform Append at Beginning of List - Python The task of appending an element to the beginning of a list involves adding a new item at the start of an existing list, shifting the other elements to the right. For example, if we have a list [1, 2, 3, 4] and we want to append the value 0 at the beginning, the resulting list would be [0, 1, 2, 3,
4 min read