Method: Using join () + list ()
This method can be used to solve this problem in one line. In this case, we simply concatenate all the elements one at a time to the target element and then convert back to a list using list ()
.
# Python3 code to demonstrate how it works # Add an element at an alternative position in the list # using join ( ) + list () # initialize the list test_list = [ ’ a’ , ’b’ , ’c’ , ’ d’ , ’e’ , ’f’ ] # print original list print ( "The original list is:" + str (test_list)) # initialize ele ele = ’#’ # Add an element at an alternative position in the list # using join () + list () res = list (ele.join (test_list)) # print result print ( "List after alternate addition:" + str (res)) |
Output:
The original list is: [’a’,’ b’, ’c’,’ d’, ’e’,’ f’] List after alternate addition: [’a’,’ # ’,’ b ’,’ # ’,’ c’, ’#’, ’d’,’ # ’,’ e’, ’#’, ’f’]