Open In App

Python program to extract characters in given range from a string list

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 list
test_list = ["geeksforgeeks", "is", "best", "for", "geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing char range
strt, end = 14, 30
 
# strt and end used to get desired characters
res = ''.join([sub for sub in test_list])[strt : end]
 
# printing result
print("Range characters : " + str(res))


Output

The original list is : ['geeksforgeeks', 'is', 'best', 'for', 'geeks']
Range characters : sbestforgeeks

Time Complexity: O(n) where n is the number of elements in the list “test_list”. 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”. 

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 module
from itertools import chain
 
# initializing list
test_list = ["geeksforgeeks", "is", "best", "for", "geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing char range
strt, end = 14, 30
 
# strt and end used to get desired characters
res = ''.join(chain.from_iterable(test_list))[strt : end]
 
# printing result
print("Range characters : " + str(res))


Output

The original list is : ['geeksforgeeks', 'is', 'best', 'for', 'geeks']
Range characters : sbestforgeeks

Time Complexity: O(n)
Space Complexity: O(n)

Approach 3: Using reduce() + join()

In this approach, we use the reduce() function from the functools module to perform the concatenation of all strings in the list. This method concatenates all strings by successively calling the lambda function.

Python3




# Approach 3: Using reduce() + join()
 
# import module
from functools import reduce
  
# initializing list
test_list = ["geeksforgeeks", "is", "best", "for", "geeks"]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing char range
strt, end = 14, 30
  
# using reduce to perform concatenation of all strings
res = reduce(lambda x, y: x + y, test_list)[strt : end]
# strt and end used to get desired characters
  
# printing result
print("Range characters : " + str(res))


Output

The original list is : ['geeksforgeeks', 'is', 'best', 'for', 'geeks']
Range characters : sbestforgeeks

Time complexity: O(n), where ‘n’ is the total number of characters in the given list.
Auxiliary space: O(m), where ‘m’ is the length of the desired substring.

Approach 4: use a simple for loop to iterate through the strings and concatenate them.

Step-by-step approach:

  • Initialize a variable ‘result’ as an empty string to store the concatenated strings.
  • Iterate through each string in the given list ‘test_list’ using a for loop.
  • Use the ‘+= operator’ to concatenate each string with the ‘result’ variable.
  • Use string slicing to get the desired characters from the concatenated string.
  • Print the desired characters.

Python3




# initializing list
test_list = ["geeksforgeeks", "is", "best", "for", "geeks"]
  
# printing original list
print("The original list is : " + str(test_list))
 
# initializing char range
strt, end = 14, 30
 
# initializing result variable
result = ""
 
# concatenate all strings in test_list
for string in test_list:
    result += string
 
# get desired characters using string slicing
res = result[strt:end]
 
# printing result
print("Range characters : " + str(res))


Output

The original list is : ['geeksforgeeks', 'is', 'best', 'for', 'geeks']
Range characters : sbestforgeeks

Time complexity: O(n), where ‘n’ is the total number of characters in the given list.
Auxiliary space: O(m), where ‘m’ is the length of the desired substring.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads