Python – Convert Integer Matrix to String Matrix

PythonServer Side ProgrammingProgramming

When it is required to convert an integer matrix to a string matrix, a list comprehension is used.

Example

Below is a demonstration of the same −

my_list = [[14, 25, 17], [40, 28, 13], [59, 44, 66], [29, 33, 16]]

print("The list is :")
print(my_list)

my_result = [[str(element) for element in index]for index in my_list]

print("The reuslt is :")
print(my_result)

Output

The list is :
[[14, 25, 17], [40, 28, 13], [59, 44, 66], [29, 33, 16]]
The reuslt is :
[['14', '25', '17'], ['40', '28', '13'], ['59', '44', '66'], ['29', '33', '16']]

Explanation

  • A list is defined and displayed on the console.

  • A list comprehension is used to iterate over the list, and every element is converted to string and is stored in a list.

  • This result is assigned to a variable.

  • This is the output that is displayed on the console.

raja
Published on 08-Sep-2021 10:33:42
Advertisements