Python | Add element at alternate position in list Last Updated : 30 Mar, 2023 Comments Improve Suggest changes Like Article Like Report 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. Comment More infoAdvertise with us Next Article Python | Add element at alternate position in list manjeet_04 Follow Improve Article Tags : Python Python list-programs Practice Tags : python 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 How to Create a List of N-Lists in Python In Python, we can have a list of many different kinds, including strings, numbers, and more. Python also allows us to create a nested list, often known as a two-dimensional list, which is a list within a list. Here we will cover different approaches to creating a list of n-lists in Python. The diffe 3 min read Extending a list in Python In Python, a list is one of the most widely used data structures for storing multiple items in a single variable. Often, we need to extend a list by adding one or more elements, either from another list or other iterable objects. Python provides several ways to achieve this. In this article, we will 2 min read Iterate over a list in Python Python provides several ways to iterate over list. The simplest and the most common way to iterate over a list is to use a for loop. This method allows us to access each element in the list directly.Example: Print all elements in the list one by one using for loop.Pythona = [1, 3, 5, 7, 9] # On each 3 min read Add array elements in Ruby In this article, we will learn how to add elements to an array in Ruby.Method #1: Using Index Ruby # Ruby program to add elements # in array # creating string using [] str = ["GFG", "G4G", "Sudo", "Geeks"] str[4] = "new_ele"; print str # in we skip t 1 min read Like