Increasing alternate element pattern in list in Python

PythonServer Side ProgrammingProgramming

In this tutorial, we are going to write a program that includes increasing order of an element to the given list after each element. Let's see an example to understand it clearly.

Input

alphabets = ['a', 'b', 'c']

Output

['a', '#', 'b', '##', 'c', '###']

Follow the below steps to solve the problem.

  • Initialize a list.
  • 3Create an empty list.
  • Iterate over the initial list.
  • Add the current element and the respective number of hashes to the empty list.
  • Print the resultant list.

Example

 Live Demo

# initializing the list
alphabets = ['a', 'b', 'c']
# empty list
result = []
# iterating over the alphabets
for i in range(len(alphabets)):
   # appending the current element
   result.append(alphabets[i])
   # appending the (i + 1) number of hashes
   result.append((i + 1) * '#')
# printing the result
print(result)

Output

If you run the above code, then you will get the following result.

['a', '#', 'b', '##', 'c', '###']

Conclusion

If you have doubts regarding the tutorial, mention them in the comment section.

raja
Updated on 07-Jul-2020 08:48:40

Advertisements