Open In App

Python | Convert list of strings and characters to list of characters

Sometimes we come forward to the problem in which we receive a list that consists of strings and characters mixed and the task we need to perform is converting that mixed list to a list consisting entirely of characters. Let’s discuss certain ways in which this is achieved. 

Method #1 : Using List comprehension In this method, we just consider each list element as string and iterate their each character and append each character to the newly created target list. 




# Python3 code to demonstrate
# to convert list of string and characters
# to list of characters
# using list comprehension
 
# initializing list
test_list = ['gfg', 'i', 's', 'be', 's', 't']
 
# printing original list
print("The original list is : " + str(test_list))
 
# using list comprehension
# to convert list of string and characters
# to list of characters
res = [i for ele in test_list for i in ele]
 
# printing result
print("The list after conversion is : " + str(res))

Output:
The original list is : ['gfg', 'i', 's', 'be', 's', 't']
The list after conversion is : ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']

Method #2 : Using join() join function can be used to open the string and then join each letter with empty string resulting in a single character extraction. The end result has to be type casted to list for desired result. 




# Python3 code to demonstrate
# to convert list of string and characters
# to list of characters
# using join()
 
# initializing list
test_list = ['gfg', 'i', 's', 'be', 's', 't']
 
# printing original list
print("The original list is : " + str(test_list))
 
# using join()
# to convert list of string and characters
# to list of characters
res = list(''.join(test_list))
 
# printing result
print("The list after conversion is : " + str(res))

Output:
The original list is : ['gfg', 'i', 's', 'be', 's', 't']
The list after conversion is : ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']

Time Complexity: O(n), where n is length of test_list.

Auxiliary Space: O(m), where m is length of res list.

Method #3 : Using chain.from_iterable() from_iterable function performs the similar task of firstly opening each string and then joining the characters one by one. This is the most pythonic way to perform this particular task. 




# Python3 code to demonstrate
# to convert list of string and characters
# to list of characters
# using chain.from_iterable()
from itertools import chain
 
# initializing list
test_list = ['gfg', 'i', 's', 'be', 's', 't']
 
# printing original list
print("The original list is : " + str(test_list))
 
# using chain.from_iterable()
# to convert list of string and characters
# to list of characters
res = list(chain.from_iterable(test_list))
 
# printing result
print("The list after conversion is : " + str(res))

Output:
The original list is : ['gfg', 'i', 's', 'be', 's', 't']
The list after conversion is : ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']

The time complexity of this code is O(n), where n is the length of the input list.

The auxiliary space complexity of this code is also O(n), where n is the length of the input list.

Method #4 : Using isinstance()+list()+extend()

You can use the extend() method in combination with a for loop to convert a list of strings and characters to a list of characters. Here’s an example:




# initializing list
test_list = ['gfg', 'i', 's', 'be', 's', 't']
 
# create an empty list to store the characters
result = []
 
# iterate through the elements in test_list
for ele in test_list:
    # if the element is a string, use extend() to add its characters to the result list
if isinstance(ele, str):
    result.extend(list(ele))
# if the element is a character, add it to the result list
else:
    result.append(ele)
 
# print the resulting list of characters
print(result)

Output
['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']

Method #5: Using map() and itertools.chain()

The map() function applies the list() function to each element of test_list, converting each string into a list of its characters. The resulting char_lists variable is an iterable of lists. Then the itertools.chain() function takes each of the lists from char_lists and concatenates them into a single list of characters. Finally, convert the concatenated characters back into a list using the list() function.




import itertools
 
# initializing list 
test_list = [ 'gfg', 'i', 's', 'be', 's', 't']
 
# use map() to apply list() to each element of test_list
# this converts each string in test_list into a list of its characters
char_lists = map(list, test_list)
 
# use itertools.chain() to concatenate all the char lists into a single list
result = list(itertools.chain(*char_lists))
 
# print the resulting list of characters
print(result)

Output
['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']

Time complexity: O(n), where n is the length of the input list. 
Auxiliary space: O(n), since we create a list of lists of characters with the same length as the input list.

Method #6: Using nested for loops

We can also use nested for loops to iterate through the strings in the list and append each character to the result list.

Step by step approach:

  1. Initialize an empty list result to store the characters.
  2. Iterate through each string in the list test_list using a for loop.
  3. Iterate through each character in the string using a nested for loop.
  4. Append each character to the result list.
  5. Print the resulting list of characters.




# initializing list
test_list = [ 'gfg', 'i', 's', 'be', 's', 't']
 
# initialize an empty list to store the characters
result = []
 
# iterate through each string in test_list
for string in test_list:
    # iterate through each character in the string
    for char in string:
        # append the character to the result list
        result.append(char)
 
# print the resulting list of characters
print(result)

Output
['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']

Time complexity: O(n^2), where n is the length of the list test_list. This is because we have nested loops, and for each string in test_list, we are iterating through each character in that string.

Auxiliary space: O(n), where n is the length of the list test_list. This is because we are creating a new list to store the characters.


Article Tags :