Change language

Python | Convert a list string to a list of nested characters

|

Method # 1: Using the comprehension list + split()
A combination of the above functions can be used to accomplish this task. In this we iterate over the list using a list comprehension and can split using split ().

# Python3 code to demonstrate how it works
# Convert the list of strings to a nested list of characters
# using split () + list comprehension

 
# initialize list

test_list = [ "a, b, c" , "gfg, is, best" , " 1, 2, 3 " ]

  
# print original list

print ( " The original list: " + str (test_list))

  
# Convert list of strings to nested list of characters
# using split () + list comprehension

res = [char.split ( ’,’ ) for char in test_list]

 
# print result

print ( "List after performing conversion:" + str (res))

Output:

The original list: [’a, b, c’, ’gfg, is, best’, ’1, 2, 3 ’]
List after performing conversion: [[’ a ’,’ b ’,’ c ’], [’ gfg ’,’ is ’,’ best ’], [’ 1 ’,’ 2 ’,’ 3 ’]]

Method # 2: Using map () + split () + lambda
Combination of the above functions can be used to accomplish this task. In this we are doing the iteration task using map () and the lambda function is used to apply split logic using split () to all the elements of the list.

# Python3 code to demonstrate how it works
# Convert a list of strings to a nested list of characters
# using map () + split () + lambda

 
# initialize the list

test_list = [ " a, b, c " , " gfg, is, best " , "1, 2, 3" ]

 
# print original list

print ( "The original list:" + str (test_list))

 
# Convert a list of strings to a nested list of characters
# using map () + split () + lambda

res = list ( map ( lambda ele: ele.split ( ’, ’ ), test_list))

 
# print result

print ( "List after performing conversion:" + str (res))

Exit:

The original list: [’a, b, c’, ’gfg, is, best’, ’1, 2, 3’]
List after performing conversion: [[’a’, ’b’, ’c’], [’gfg’, ’is’, ’best’], [’1’, ’2’, ’3’]]

Python | Convert a list string to a list of nested characters Python functions: Questions

Python | Convert a list string to a list of nested characters String Variables: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method