Python - Increasing alternate element pattern in list

PythonServer Side ProgrammingProgramming

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 "" The enumerate() method adds counter to the iterable. You can read more about enumerate here ""


 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))


Printing my_list list : [1, 2, 3]
The increasing element pattern IS : [1, '*', 2, '**', 3, '***']
Published on 16-May-2020 10:31:36