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

23 Jan, 2019
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 comprehensionIn 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 listprint ("The original list is : " + str(test_list))  # using list comprehension# to convert list of string and characters# to list of charactersres = [i for ele in test_list for i in ele]  # printing result print ("The list after conversion is : " +  str(res))
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 listprint ("The original list is : " + str(test_list))  # using join()# to convert list of string and characters# to list of charactersres = list(''.join(test_list))  # printing result print ("The list after conversion is : " +  str(res))
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 #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 listprint ("The original list is : " + str(test_list))  # using chain.from_iterable()# to convert list of string and characters# to list of charactersres = list(chain.from_iterable(test_list))  # printing result print ("The list after conversion is : " +  str(res))
The original list is : ['gfg', 'i', 's', 'be', 's', 't']
The list after conversion is : ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']

Python list-programs
python-string
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python Program for Fibonacci numbers
Python | Split string into list of characters