
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Incremental Slice Concatenation in String List
When it is required to display incremental slice concatenation in string list, a simple iteration and list slicing is used.
Below is a demonstration of the same −
Example
my_list = ['pyt', 'is', 'all', 'fun'] print("The list is :") print(my_list) my_result = '' for index in range(len(my_list)): my_result += my_list[index][:index + 1] print("The result is :") print(my_result)
Output
The list is : ['pyt', 'is', 'all', 'fun'] The result is : pisallfun
Explanation
A list is defined and displayed on the console.
An empty string is created.
The list is iterated over, and the element is concatenated with the consecutive element.
This result is assigned to a variable.
This is the output that is displayed on the console.
Advertisements