Sometimes, while working with Python list, we can have a problem in which we need to extend a list in a very customized way. We may have to repeat contents of list and while doing that, each time new list must add a number to original list. This incremental expansion has applications in many domains. Let’s discuss a way in which this task can be performed.
Method : Using list comprehension
This task can be performed in a brute manner, but having a shorter implementation using list comprehension always is better. In this, we perform task in 2 steps, first we make a helper list to form a addition factor list and then cumulate the result using original list.
The original list is : [7, 8, 9] List after extension and addition : [7, 8, 9, 10, 11, 12, 16, 17, 18, 34, 35, 36]
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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course