Open In App

Python | Find Mixed Combinations of string and list

Last Updated : 07 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python, we can have a problem in which we need to make combinations of string and character list. This type of problem can come in domains in which we need to interleave the data. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using loop + enumerate() + replace() 

This task can be performed using combination of above functions. In this, we just iterate each element of character list and insert each combination using brute force way. 

step-by-step approach :

  1. Initialize a list called ‘test_list’ with the elements [“a”, “b”, “c”] and a string called ‘test_str’ with the value “gfg”.
  2. Print the original list and string using the print() function and string concatenation.
  3. Initialize an empty list called ‘res’.
  4. Use a for loop with the enumerate() function to iterate through each character in the string along with its index.
  5. Use another for loop to iterate through each character in the list.
  6. Replace the current character in the string with the current character in the list using the replace() function.
  7. Append the resulting string to the ‘res’ list.
  8. Concatenate the original substring before the current index and the modified substring after the current index and append it to the ‘res’ list.
  9. Print the resulting list using the print() function and string concatenation.

Python3




# Python3 code to demonstrate working of
# Mixed Combinations of string and list
# using loop + enumerate() + replace()
 
# initialize list and string
test_list = ["a", "b", "c"]
test_str = "gfg"
 
# printing original list and string
print("The original list : " + str(test_list))
print("The original string : " + test_str)
 
# Mixed Combinations of string and list
# using loop + enumerate() + replace()
res = []
for idx, ele in enumerate(test_str):
    res += [test_str[: idx] + test_str[idx:].replace(ele, k, 1)
            for k in test_list]
 
# printing result
print("The list after mixed Combinations : " + str(res))


Output : 

The original list : [‘a’, ‘b’, ‘c’] The original string : gfg The list after mixed Combinations : [‘afg’, ‘bfg’, ‘cfg’, ‘gag’, ‘gbg’, ‘gcg’, ‘gfa’, ‘gfb’, ‘gfc’]

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

Method #2: Using loop + enumerate() + replace() 

The above functionality can be used to perform this task. In this, we provide a one-line alternative using comprehension. 

Python3




# Python3 code to demonstrate working of
# Mixed Combinations of string and list
# using list comprehension
 
# initialize list and string
test_list = ["a", "b", "c"]
test_str = "gfg"
 
# printing original list and string
print("The original list : " + str(test_list))
print("The original string : " + test_str)
 
# Mixed Combinations of string and list
# using list comprehension
res = [test_str[ : idx] + ele + test_str[idx + 1 : ]\
      for idx in range(len(test_str)) for ele in test_list]
 
# printing result
print("The list after mixed Combinations : " + str(res))


Output : 

The original list : [‘a’, ‘b’, ‘c’] The original string : gfg The list after mixed Combinations : [‘afg’, ‘bfg’, ‘cfg’, ‘gag’, ‘gbg’, ‘gcg’, ‘gfa’, ‘gfb’, ‘gfc’]

Time complexity: O(n^2), where n is the length of the string.
Auxiliary space: O(n^2), because the list comprehension is creating a new list with n * m elements, where n and m are the lengths of the string and the list, respectively.

Method #3: Using itertools.product() and string concatenation.

Algorithm:

  1. Import the itertools module for generating the Cartesian product of the input iterables.
  2. Initialize the list and string to be mixed as test_list and test_str, respectively.
  3. Print the original list and string.
  4. Generate the mixed combinations using list comprehension and the itertools.product() function.
  5. Print the resulting mixed combinations list.

Python3




import itertools
 
# initialize list and string
test_list = ["a", "b", "c"]
test_str = "gfg"
 
# printing original list and string
print("The original list : " + str(test_list))
print("The original string : " + test_str)
 
res = [test_str[:i] + char + test_str[i+1:]
       for i, char in itertools.product(range(len(test_str)), test_list)]
 
# printing result
print("The list after mixed Combinations : " + str(res))


Output

The original list : ['a', 'b', 'c']
The original string : gfg
The list after mixed Combinations : ['afg', 'bfg', 'cfg', 'gag', 'gbg', 'gcg', 'gfa', 'gfb', 'gfc']

Time Complexity: O(NM), where N is the length of the input string test_str, and M is the length of the input list test_list. This is because we are generating all possible combinations of the input string and list, and there are NM such combinations. 

Auxiliary Space: O(NM), as we are storing all of these combinations in a list. However, note that the itertools.product() function generates the combinations lazily, meaning that it doesn’t generate the entire list all at once, but rather generates each element of the list on-the-fly as it is needed. Therefore, the actual memory usage of this algorithm may be much lower than the theoretical upper bound of O(NM).

Method #4: Using nested list comprehension and string concatenation.

Approach:

  1. Initialize a list test_list and a string test_str.
  2. Print the original list and string.
  3. Use nested list comprehension to create a new list res.
  4. In the outer loop, iterate over the indices of the characters in test_str.
  5. In the inner loop, iterate over the elements in test_list.
  6. Use string concatenation to create a new string by replacing the character at the current index with the current element from test_list.
  7. Append the new string to the result list.
  8. Print the list after mixed combinations

Python3




# initialize list and string
test_list = ["a", "b", "c"]
test_str = "gfg"
 
# printing original list and string
print("The original list : " + str(test_list))
print("The original string : " + test_str)
 
# using nested list comprehension and string concatenation
res = [test_str[:i] + char + test_str[i+1:] for i in range(len(test_str))
       for char in test_list]
 
# printing result
print("The list after mixed Combinations : " + str(res))


Output

The original list : ['a', 'b', 'c']
The original string : gfg
The list after mixed Combinations : ['afg', 'bfg', 'cfg', 'gag', 'gbg', 'gcg', 'gfa', 'gfb', 'gfc']

Time complexity: O(n^2), where n is the length of the original string test_str.
Auxiliary space: O(n^2), because we are creating a new list with n^2 elements.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads