Sometimes, while working with Python strings, we can have problem in which we need to convert String list which have strings that are joined by deliminitor to String Matrix by separation by deliminitor. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop + split()
This is one of way in which this task can be performed. In this, we iterate for each string and perform the split using split().
# Python3 code to demonstrate working of # Delimited String List to String Matrix # Using loop + split() # initializing list test_list = ['gfg:is:best', 'for:all', 'geeks:and:CS'] # printing original list print("The original list is : " + str(test_list)) # Delimited String List to String Matrix # Using loop + split() res = [] for sub in test_list: res.append(sub.split(':')) # printing result print("The list after conversion : " + str(res)) |
The original list is : ['gfg:is:best', 'for:all', 'geeks:and:CS'] The list after conversion : [['gfg', 'is', 'best'], ['for', 'all'], ['geeks', 'and', 'CS']]
Method #2 : Using list comprehension + split()
Yet another way to perform this task, is just modification of above method. In this, we use list comprehension as shorthand and one-liner to perform this.
# Python3 code to demonstrate working of # Delimited String List to String Matrix # Using list comprehension + split() # initializing list test_list = ['gfg:is:best', 'for:all', 'geeks:and:CS'] # printing original list print("The original list is : " + str(test_list)) # Delimited String List to String Matrix # Using list comprehension + split() res = [sub.split(':') for sub in test_list] # printing result print("The list after conversion : " + str(res)) |
The original list is : ['gfg:is:best', 'for:all', 'geeks:and:CS'] The list after conversion : [['gfg', 'is', 'best'], ['for', 'all'], ['geeks', 'and', 'CS']]
Recommended Posts:
- Python - Check if Elements delimited by K
- Python | Convert List of String List to String List
- Python | Convert list of tuples to list of list
- Python | String List to Column Character Matrix
- Python program to Convert a Matrix to Sparse Matrix
- Python - Convert Matrix to Custom Tuple Matrix
- Python | Convert a string representation of list into list
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Convert string enclosed list to list
- Python | Convert mixed data types tuple list to string list
- Python | Convert string List to Nested Character List
- Python - Convert String List to Key-Value List dictionary
- Python - Filter the List of String whose index in second List contaons the given Substring
- Python | Type casting whole List and Matrix
- python | Nested List Intersection Matrix Product
- Python - Group elements from Dual List Matrix
- Python - List Strings frequency in Matrix
- Python - Merge List value Keys to Matrix
- Python - Convert Tuple Matrix to Tuple List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

