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
The original list is : ['a', 'b', 'c', 'd', 'e', 'f'] List after alternate addition : ['a', '#', 'b', '#', 'c', '#', 'd', '#', 'e', '#', 'f']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.