Open In App

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

Last Updated : 06 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Using itertools.chain()

itertools.chain() combines multiple lists or iterables into one, without creating extra copies of the data. It’s fast and memory-efficient because it processes the items one by one, directly from the source.

Python
import itertools

# Flatten the list of strings into individual characters
li = ['gfg', 'i', 's', 'be', 's', 't']

 # Unpacks and combines the strings into one iterable
res = list(itertools.chain(*li)) 

print(res)

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

Explanation:

  • *li: This unpacks the list so each string is passed as a separate argument to chain.
  • itertools.chain(*li): This combines all characters from the strings into one iterable.
  • list(): This converts the iterable into a list of characters.

Using List comprehension

List comprehension lets us quickly create a new list by looping through an existing list. It’s a simple way to flatten nested lists or transform data, all in one line, making our code cleaner and easier to read.

Python
li= ['gfg', 'i', 's', 'be', 's', 't']

res = [i for ele in li for i in ele]
print(res)

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

Explanation:

  • for ele in li: This loops through each string in the list li.
  • for i in ele: This loops through each character in the current string ele.
  • [i for ele in li for i in ele]:This combines both loops into one, creating a list of all characters from all strings in li.

Using join()

join() method combines all the strings in a list into one long string. After that, we turn this single string into a list of individual characters. It’s a bit less efficient than the other methods because it first combines everything into one string before breaking it down into characters.

Python
li = ['gfg', 'i', 's', 'be', 's', 't']

res = list(''.join(li))
print(res)

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

Explanation:

  • ”.join(li): This combines all the strings in the list into one long string without any spaces, resulting in ‘gfgisbest’.
  • list(”.join(li)): This converts that long string into a list of individual characters .

Using map()

map() turn each string into a list of characters, and then itertools.chain() merges them into one continuous sequence. It’s effective but less efficient than chain.from_iterable() because it creates extra intermediate lists, using more memory.

Python
from itertools import chain
li = ['gfg', 'i', 's', 'be', 's', 't']

res = list(chain(*map(list, li)))
print(res)

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

Explanation:

  • map(list, li): This converts each string to a list of characters.
  • chain(*...): This flattens the lists into a single iterable.
  • list(): This converts the iterable into a list.

Using for loop ()

nested for loop method is slow for large datasets because it has to go through each character in every string in the list, resulting in many extra iterations. This makes it the least efficient approach, especially when dealing with big lists.

Python
li = ['gfg', 'i', 's', 'be', 's', 't']

# Create an empty list to store result
res = []
for ele in li:
    for i in ele:
        res.append(i)
print(res)

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

Explanation:

  • for ele in li : This loops through each string in the list.
  • for i in ele: This loops through each character in the current string ele.
  • res.append(i): This appends each character i to the res list.


Next Article

Similar Reads