Python program to extract characters in given range from a string list
Last Updated :
02 May, 2023
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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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.
Similar Reads
Python - Extract only characters from given string
To extract only characters (letters) from a given string we can use various easy and efficient methods in Python. Using str.isalpha() in a Loop str.isalpha() method checks if a character in a string is an alphabetic letter. Using a loop, we can iterate through each character in a string to filter ou
2 min read
Python program to remove last N characters from a string
In this article, weâll explore different ways to remove the last N characters from a string in Python. This common string manipulation task can be achieved using slicing, loops, or built-in methods for efficient and flexible solutions. Using String SlicingString slicing is one of the simplest and mo
2 min read
Python Program to Extract Strings with at least given number of characters from other list
Given a list containing only string elements, the task is to write a Python program to extract all the strings which have characters from another list given a number of times. Examples: Input : test_list = ["Geeksforgeeks", "is", "best", "for", "geeks"], char_list = ['e', 't', 's', 'm', 'n'], K = 2
7 min read
Python - Extract range characters from String
Given a String, extract characters only which lie between given letters. Input : test_str = 'geekforgeeks is best', strt, end = "g", "s" Output : gkorgksiss Explanation : All characters after g and before s are retained. Input : test_str = 'geekforgeeks is best', strt, end = "g", "r" Output : gkorgk
4 min read
Python program for removing i-th character from a string
In this article, we will explore different methods for removing the i-th character from a string in Python. The simplest method involves using string slicing. Using String SlicingString slicing allows us to create a substring by specifying the start and end index. Here, we use two slices to exclude
2 min read
Python program to remove the nth index character from a non-empty string
Given a String, the task is to write a Python program to remove the nth index character from a non-empty string Examples: Input: str = "Stable" Output: Modified string after removing 4 th character Stabe Input: str = "Arrow" Output: Modified string after removing 4 th character Arro The first approa
4 min read
Python - Extract String till all occurrence of characters from other string
Given a string, the task is to write a Python program to extract till all characters from other strings are found. Input : test_str = "geeksforgeeks is best for all geeks", check_str = "freak"Output : geeksforgeeks is best for aExplanation : a is last letter in freak to be present in string. The str
11 min read
Python program to print k characters then skip k characters in a string
Given a String, extract K characters alternatively. Input : test_str = 'geeksgeeksisbestforgeeks', K = 4 Output : geekksisforg Explanation : Every 4th alternate range is sliced. Input : test_str = 'geeksgeeksisbest', K = 4 Output : geekksis Explanation : Every 4th alternate range is sliced. Method #
5 min read
Python - Remove front K characters from each string in String List
Sometimes, we come across an issue in which we require to delete the first K characters from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is always a
6 min read
Python | Remove given character from Strings list
Sometimes, while working with Python list, we can have a problem in which we need to remove a particular character from each string from list. This kind of application can come in many domains. Let's discuss certain ways to solve this problem. Method #1 : Using replace() + enumerate() + loop This is
8 min read