Python - Convert Strings to Character Matrix
Last Updated : 23 Mar, 2023
Sometimes, while dealing with String lists, we can have a problem in which we need to convert the Strings in list to separate character list. Overall converting into Matrix. This can have multiple applications in data science domain in which we deal with lots of data. Lets discuss certain ways in which this task can be performed.
Method #1 : Using List comprehension This is one way in which this task can be performed. In this, we convert the string to list of characters using list() and convert the result to desired matrix.
Python3 # Python3 code to demonstrate # Convert Strings to Character Matrix # using List comprehension # Initializing list test_list = ['gfg', 'is', 'best'] # printing original list print("The original list is : " + str(test_list)) # Convert Strings to Character Matrix # using List comprehension res = [list(sub) for sub in test_list] # printing result print ("List String after conversion to Matrix : " + str(res))
OutputThe original list is : ['gfg', 'is', 'best'] List String after conversion to Matrix : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2 : Using loop + list() This is brute force way to perform this task. In this, we run loop and convert each string to character list using list().
Python3 # Python3 code to demonstrate # Convert Strings to Character Matrix # using loop + list() # Initializing list test_list = ['gfg', 'is', 'best'] # printing original list print("The original list is : " + str(test_list)) # Convert Strings to Character Matrix # using loop + list() res = [] for sub in test_list: res.append(list(sub)) # printing result print ("List String after conversion to Matrix : " + str(res))
OutputThe original list is : ['gfg', 'is', 'best'] List String after conversion to Matrix : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
Method #3 : Here's another approach using the map function:
Python3 # Python3 code to demonstrate # Convert Strings to Character Matrix # using map() # Initializing list test_list = ['gfg', 'is', 'best'] # printing original list print("The original list is : " + str(test_list)) # Convert Strings to Character Matrix # using map() res = list(map(list, test_list)) # printing result print ("List String after conversion to Matrix : " + str(res)) #This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list is : ['gfg', 'is', 'best'] List String after conversion to Matrix : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
Time Complexity: O(n) where n is the length of the test_list.
Auxiliary Space: O(n) as we are creating a new list res to store the result.
Explanation: In this approach, we use the map function to apply the list function to each element of the test_list and store the result in the res list.
Similar Reads
Convert Character Matrix to single String - Python In this article, we will explore various methods to convert character matrix to single string in Python. The simplest way to do is by using a loop.Using a LoopWe can use a loop (for loop) to iterate through each sublist and concatenate the elements of each sublist and then combine them into a final
2 min read
Python | String List to Column Character Matrix Sometimes, while working with Python lists, we can have a problem in which we need to convert the string list to Character Matrix where each row is String list column. This can have possible application in data domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using
5 min read
Python - Convert String to matrix having K characters per row Given a String, convert it to Matrix, having K characters in each row. Input : test_str = 'GeeksforGeeks is best', K = 7 Output : [['G', 'e', 'e', 'k', 's', 'f', 'o'], ['r', 'G', 'e', 'e', 'k', 's', ' '], ['i', 's', ' ', 'b', 'e', 's', 't']] Explanation : Each character is assigned to 7 element row
9 min read
Python - Convert String to unicode characters Convert String to Unicode characters means transforming a string into its corresponding Unicode representations. Unicode is a standard for encoding characters, assigning a unique code point to every character.For example:string "A" has the Unicode code point U+0041.string "ä½ å¥½" corresponds to U+4F60
2 min read
Python - Combine Strings to Matrix Sometimes while working with data, we can receive separate data in the form of strings and we need to compile them into Matrix for its further use. This can have applications in many domains. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + split
4 min read
Convert String to bytes-Python The goal here is to convert a string into bytes in Python. This is essential for working with binary data or when encoding strings for storage or transmission. For example, given the string "Hello", these methods can convert it into a byte representation like b'Hello'. Letâs explore different method
2 min read