Summary: There are three different ways to split a given string by the list of indices:
🍎 Slice Within a For loop
zip and then Slice
zip_longest and then Slice
# 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: Given a string; How will you split the string by a list of indices?
# 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
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]])
I want to learn Python
- Append a
Nonevalue to the list of indices. This will be instrumental while slicing the required substrings from the given string. Use a
forloop 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
Nonethe 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:
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
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)].
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 is that you do not have to append
None explicitly to the list of indices.
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
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:
text = "I want to learn Python" indices = [0, 5, 8, 14, 21]
I I want I want to I want to learn I want to learn Python
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.
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!
But before we move on, I’m excited to present you 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).