- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - To Convert Matrix to String
When it is required to convert a matrix into a string, a simple list comprehension along with the ‘join’ method is used.
Example
Below is a demonstration of the same
my_list = [[1, 22, "python"], [22, "is", 1], ["great", 1, 91]] print("The list is :") print(my_list) my_list_1, my_list_2 = ",", " " my_result = my_list_2.join([my_list_1.join([str(elem) for elem in sub]) for sub in my_list]) print("The result is :") print(my_result)
Output
The list is : [[1, 22, 'python'], [22, 'is', 1], ['great', 1, 91]] The result is : 1,22,python 22,is,1 great,1,91
Explanation
A list of list is defined and is displayed on the console.
Two variables are assigned to a comma and an empty space respectively.
The list comprehension and ‘join’ method is used to iterate through the list and join the elements in two variables.
This is assigned to a variable.
This is the output that is displayed on the console.
- Related Articles
- Python – Convert Integer Matrix to String Matrix
- Python Program to Convert Matrix to String
- Python – Convert String to matrix having K characters per row
- Python Program – Convert String to matrix having K characters per row
- Python program to Convert Matrix to Dictionary Value List
- Convert String to Tuple in Python
- Python program to convert a list to string
- How to convert string to binary in Python?
- How to convert string to JSON using Python?
- Python program to convert hex string to decimal
- Python - Ways to convert string to json object
- How to convert list to string in Python?
- How to convert int to string in Python?
- Convert string dictionary to dictionary in Python
- Convert date string to timestamp in Python

Advertisements