Summary: There are three different ways to split a given string by the list of indices:
π Slice Within a For loop
π Use zip
and then Slice
π Use zip_longest
and then Slice
Minimal Example
# Method 1 text = "Hello and Welcome to Finxter" indices = [0, 6, 18] indices.append(None) for i in range(len(indices) - 1): print(text[indices[i]:indices[i + 1]]) # Method 2 text = "Hello and Welcome to Finxter" indices = [0, 6, 18] res = [text[i:j] for i,j in zip(indices, indices[1:]+[None])] print(res) # Method 3 from itertools import zip_longest text = "Hello and Welcome to Finxter" indices = [0, 6, 18] print([text[i:j] for i,j in zip_longest(indices, indices[1:])]) # Output: ['Hello ', 'and Welcome ', 'to Finxter']
Problem Formulation
πProblem: Given a string; How will you split the string by a list of indices?
Example
# Input text = "I want to learn Python" indices = [0, 2, 7] # Expected Output ['I ', 'want ', 'to learn Python']
The start of each substring, i.e., the index at which you have to split the string is given along with the string itself. Can you split the string using the indices given?
Now that we have an overview of our problem let us dive into the solutions without further ado.
Method 1: Slice Within a For Loop
Slicing is the concept to carve out a substring from a given string. Use slicing notation s[start:stop:step]
to access every step-th
element starting from index start
(included) and ending in index stop
(excluded).
Code:
text = "I want to learn Python" indices = [0, 2, 7] indices.append(None) for i in range(len(indices) - 1): print(text[indices[i]:indices[i + 1]])
Output:
I want to learn Python
Explanation:
- Append a
None
value to the list of indices. This will be instrumental while slicing the required substrings from the given string. Use afor
loop to iterate through the list containing the indices. - Slice the given string using appropriate start and stop index. The start index can be fetched by accessing the index from the list containing indices using the for loop iterator variable as : “
index[i]
“, and the stop index is the next value of the indices list given by “index[i+1]
” index value in the list. - Note that appending a “None” to the indices list ensures that last section of the substring (starting from index 7 until end of string) is taken into account and the split operation is performed accurately. In absence of
None
the script won’t be able to slice the string in the last iteration and you won’t be able to fetch the last part of the string.
πAlternate Formulation Using List Comprehension
The above solution can be formulated in a more compact way using a list comprehension as shown in the solution below:
Code:
text = "I want to learn Python" indices = [0, 2, 7] indices.append(None); [print([text[indices[i]:indices[i+1]] for i in range(len(indices)-1)])]
Note that the concept/logic used in the above code is the same as used in the multi-line solution previously.
List comprehension is a compact way of creating lists. The simple formula is [expression + context]. The example [x for x in range(3)] creates the list [0, 1, 2].
πRelated Read: List Comprehension in Python β A Helpful Illustrated Guide
Method 2: Zip and Slice
Prerequisite: The zip()
function takes an arbitrary number of iterables and aggregates them to a single iterable, a zip
object. It combines the i-th values of each iterable argument into a tuple. Hence, if you pass two iterables, each tuple will contain two values. If you pass three iterables, each tuple will contain three values. For example, zip
together lists [1, 2, 3]
and [4, 5, 6] to [(1,4), (2,5), (3,6)].
Code:
text = "I want to learn Python" indices = [0, 2, 7] res = [text[i:j] for i,j in zip(indices, indices[1:]+[None])] print(res)
Explanation: The expression (indices[1:] + [None]
) copies the list without the first element and adds a None
at the end of the list. The zip()
method uses the indices as the iterable and creates a tuple. The tuple consists of two values- indices and the indices of the copied list. Using the for loop we iterate through the tuples containing the pair of required indices and slice the given string to get the required output.
πRelated Read: Python Zip β A Helpful Illustrated Guide
Method 3: Using zip_longest
Approach: You can use the zip_longest
method from the itertools module to split the given string by the list of indices. The zip_longest
method groups the elements at the same position in each list and allows you to use the remaining elements of the longer list into consideration. Thus, the advantage of using zip_longest
over zip
is that you do not have to append None
explicitly to the list of indices.
Code:
from itertools import zip_longest text = "I want to learn Python" indices = [0, 2, 7] print([text[i:j] for i,j in zip_longest(indices, indices[1:])])
πRelated Read: Using zip_longest from Itertools Module
ππΌExercise
Now that you have gone through the numerous ways of splitting a string using a list of indices, here’s a programming challenge for you to test your grasp on the topic.
Challenge: Challenge: You have been given a string and a list of indices. Split the string in such a way that the output is as follows:
Input:
text = "I want to learn Python" indices = [0, 5, 8, 14, 21]
Expected Output:
I I want I want to I want to learn I want to learn Python
Solution:
text = "I want to learn Python" indices = [0, 5, 8, 14, 21] parts = [text[:index + 1] for index in indices] for part in parts: print(part)
Explanation: You have to slice the string in a way that the start index of every element is always zero. The end index gets calculated with the help of a for
loop in which we iterate through all the elements in the given list indices. The resultant output is the list of strings starting from the beginning of the string until the specified indices in the list containing the indices. Print each element of the list individually to get the display output.
Conclusion
Hurrah! We have successfully solved the given problem using as many as three different ways. We also solved a similar coding challenege to enhance our grip on the topic. I hope you enjoyed this article and it helps you in your Python coding journey. Please subscribe and stay tuned for more interesting articles!
Check out my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Publisher Link: https://nostarch.com/pythononeliners