Python - Increasing alternate element pattern in list


In this we are going to use List Comprehension with enumerate() Python provides compact syntax for deriving one list from another. These expressions are called list comprehensions.List comprehensions are one of the most powerful tools in Python. Python’s list comprehension is an example of the language’s support for functional programming concepts. You can read more about it here "www.tutorialspoint.com/python-list-comprehension" The enumerate() method adds counter to the iterable. You can read more about enumerate here " www.tutorialspoint.com/enumerate-in-python"

Example

 Live Demo

# declare list of integers
my_list = [1, 2, 3]
# printing the value
print("Printing my_list list : " + str(my_list))
response = [value for sub in ((i, "*" * j)
   for j, i in enumerate(my_list, 1))
   for value in sub]
# print result
print("The increasing element pattern IS : " + str(response))

Output

Printing my_list list : [1, 2, 3]
The increasing element pattern IS : [1, '*', 2, '**', 3, '***']

Updated on: 16-May-2020

80 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements