Python program to extract characters in given range from a string list
Given a Strings List, extract characters in index range spanning entire Strings list.
Input : test_list = ["geeksforgeeks", "is", "best", "for", "geeks"], strt, end = 14, 20 Output : sbest Explanation : Once concatenated, 14 - 20 range is extracted.
Input : test_list = ["geeksforgeeks", "is", "best", "for", "geeks"], strt, end = 11, 20 Output : sbesbest Explanation : Once concatenated, 11 – 20 range is extracted.
Method 1 : Using join() + list comprehension
In this, we perform concatenation of all the strings using join() and list comprehension, and post that, the required character range is extracted.
Python3
# initializing listtest_list = ["geeksforgeeks", "is", "best", "for", "geeks"]# printing original listprint("The original list is : " + str(test_list))# initializing char rangestrt, end = 14, 30# strt and end used to get desired charactersres = ''.join([sub for sub in test_list])[strt : end]# printing resultprint("Range characters : " + str(res)) |
Output
The original list is : ['geeksforgeeks', 'is', 'best', 'for', 'geeks'] Range characters : sbestforgeeks
Method 2: Using chain.from_iterable() + join()
In this, we perform the task of flattening of characters using chain.from_iterable(), after that join() is used for concatenation of all strings, and indices are extracted in range.
Python3
# import modulefrom itertools import chain# initializing listtest_list = ["geeksforgeeks", "is", "best", "for", "geeks"]# printing original listprint("The original list is : " + str(test_list))# initializing char rangestrt, end = 14, 30# strt and end used to get desired charactersres = ''.join(chain.from_iterable(test_list))[strt : end]# printing resultprint("Range characters : " + str(res)) |
Output
The original list is : ['geeksforgeeks', 'is', 'best', 'for', 'geeks'] Range characters : sbestforgeeks
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)


