Saturday, November 16, 2024
Google search engine
HomeLanguagesPython – Extract words starting with K in String List

Python – Extract words starting with K in String List

Given list of phrases, extract all the Strings with begin with character K.

Input : test_list = [“Gfg is good for learning”, “Gfg is for Lazyroar”, “I love G4G”], K = l 
Output : [‘learning’, ‘love’] 
Explanation : All elements with L as starting letter are extracted. 

Input : test_list = [“Gfg is good for learning”, “Gfg is for Lazyroar”, “I love G4G”], K = m 
Output : [] 
Explanation : No words started from “m” hence no word extracted.

Method #1 : Using loop + split()

This is brute way in which this problem can be solved. In this, we convert each phrase into list of words and then for each word, check if it’s initial character is K. 

Python3




# Python3 code to demonstrate working of
# Extract words starting with K in String List
# Using loop + split()
 
# initializing list
test_list = ["Gfg is best", "Gfg is for Lazyroar", "I love G4G"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = "g"
 
res = []
for sub in test_list:
    # splitting phrases
    temp = sub.split()
    for ele in temp:
 
        # checking for matching elements
        if ele[0].lower() == K.lower():
            res.append(ele)
 
# printing result
print("The filtered elements : " + str(res))


Output

The original list is : ['Gfg is best', 'Gfg is for Lazyroar', 'I love G4G']
The filtered elements : ['Gfg', 'Gfg', 'Lazyroar', 'G4G']

Time Complexity: O(n*n), where n is the size of list
Auxiliary Space: O(n), where n is the size of list

Method #2 : Using list comprehension + split() 

This is yet another way in which this task can be performed. In this we run double nested loops inside single list comprehension and perform required conditional checks.

Python3




# Python3 code to demonstrate working of
# Extract words starting with K in String List
# Using list comprehension + split()
 
# initializing list
test_list = ["Gfg is best", "Gfg is for Lazyroar", "I love G4G"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = "g"
res = [ele for temp in test_list for ele in temp.split() if ele[0].lower()
       == K.lower()]
 
# printing result
print("The filtered elements : " + str(res))


Output

The original list is : ['Gfg is best', 'Gfg is for Lazyroar', 'I love G4G']
The filtered elements : ['Gfg', 'Gfg', 'Lazyroar', 'G4G']

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

Method #3 : Using split(),extend(),lower() and find() methods

Python3




# Python3 code to demonstrate working of
# Extract words starting with K in String List
 
# initializing list
test_list = ["Gfg is best", "Gfg is for Lazyroar", "I love G4G"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = "g"
 
res = []
x = []
for i in test_list:
    x.extend(i.split())
for j in x:
    if j.lower().find(K) == 0:
        res.append(j)
 
# printing result
print("The filtered elements : " + str(res))


Output

The original list is : ['Gfg is best', 'Gfg is for Lazyroar', 'I love G4G']
The filtered elements : ['Gfg', 'Gfg', 'Lazyroar', 'G4G']

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. 

Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.

Method #4 : Using startswith(),upper() and lower() methods

Python3




# Python3 code to demonstrate working of
# Extract words starting with K in String List
# Using loop + split()
 
# initializing list
test_list = ["Gfg is best", "Gfg is for Lazyroar", "I love G4G"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = "g"
 
res = []
x = []
for i in test_list:
    x.extend(i.split())
for i in x:
    if(i.startswith(K) or i.startswith(K.lower()) or i.startswith(K.upper())):
        res.append(i)
 
# printing result
print("The filtered elements : " + str(res))


Output

The original list is : ['Gfg is best', 'Gfg is for Lazyroar', 'I love G4G']
The filtered elements : ['Gfg', 'Gfg', 'Lazyroar', 'G4G']

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

Method #4 : Using lambda functions, extend(), lower() methods

Python3




# Python3 code to demonstrate working of
# Extract words starting with K in String List
 
# initializing list
test_list = ["Gfg is best", "Gfg is for Lazyroar", "I love G4G"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = "g"
 
res = []
for i in test_list:
    words = i.split()
    startWords = list(filter(lambda x: x[0].lower() == K.lower(), words))
    res.extend(startWords)
 
# printing result
print("The filtered elements : " + str(res))


Output

The original list is : ['Gfg is best', 'Gfg is for Lazyroar', 'I love G4G']
The filtered elements : ['Gfg', 'Gfg', 'Lazyroar', 'G4G']

Time Complexity: O(N*M)

Auxiliary Space: O(N*M)

Method #5 : Using itertools.filterfalse() functions

Python3




# Python3 code to demonstrate working of
# Extract words starting with K in String List
import itertools
# initializing list
test_list = ["Gfg is best", "Gfg is for Lazyroar", "I love G4G"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = "g"
 
res = []
for i in test_list:
    words = i.split()
    startWords = list(itertools.filterfalse(lambda x: x[0].lower() != K.lower(), words))
    res.extend(startWords)
 
# printing result
print("The filtered elements : " + str(res))


Output

The original list is : ['Gfg is best', 'Gfg is for Lazyroar', 'I love G4G']
The filtered elements : ['Gfg', 'Gfg', 'Lazyroar', 'G4G']

Time Complexity: O(N*M)
Auxiliary Space: O(N*M)

Method #6:Using reduce()

  1. Import the reduce() function from the functools module.
  2. Initialize the list of strings test_list and the character K.
  3. Use reduce() with a lambda function that takes two arguments x and y.
  4. Split each string y in the list into words and filter the words that start with the character K.
  5. Append the filtered words to the accumulator list x.
  6. Initialize the initial value of the accumulator as an empty list [].
  7. The final result list will be the value returned by the reduce() function.
  8. Print the final result list.

Python3




# importing reduce module
from functools import reduce
 
# initializing list
test_list = ["Gfg is best", "Gfg is for Lazyroar", "I love G4G"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = "g"
 
# Extract words starting with K in String List
# Using reduce() + split()
res = reduce(lambda x, y: x + [ele for ele in y.split() if ele[0].lower() == K.lower()], test_list, [])
 
# printing result
print("The filtered elements : " + str(res))
 
#This code is contributed by Vinay Pinjala.


Output

The original list is : ['Gfg is best', 'Gfg is for Lazyroar', 'I love G4G']
The filtered elements : ['Gfg', 'Gfg', 'Lazyroar', 'G4G']

Time Complexity: O(n*m)

where n is the number of strings in test_list and m is the maximum number of words in any string.
The time complexity of split() is O(m) and the time complexity of filtering words starting with K is also O(m).
Since this process is done for each string in test_list, the overall time complexity is O(n*m).

Space Complexity: O(n*m)

The space complexity is dominated by the list filtered_words which stores the filtered words.
The maximum size of this list can be n*m, if all the words in test_list start with the character K.
The space complexity of reduce() is O(m) because it only stores the intermediate result (the list of filtered words) during each iteration.
Therefore, the overall space complexity is O(n*m).

RELATED ARTICLES

Most Popular

Recent Comments